HomeCSharpC# Error CS0133 – The expression being assigned to ‘variable’ must be constant

C# Error CS0133 – The expression being assigned to ‘variable’ must be constant

C# Compiler Error

CS0133 – The expression being assigned to ‘variable’ must be constant

Reason for the Error

You will receive this error when you assign a static variable to a constant where variable is not of type “constant” in your C# code.

For example, try compiling the below C# code snippet.

namespace DeveloperPublishNamespace
{ 
    public class DeveloperPublish
    {
        public const int i = j;
        public static int j =10;
        public static void Main()
        {
            
        }
    }
}

You will receive the error CS0133 because j (of type static int) is assigned to a constant i.

Error CS0133 The expression being assigned to ‘DeveloperPublish.i’ must be constant ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 5 Active

C# Error CS0133 – The expression being assigned to 'variable' must be constant

Solution

To fix the error code CS0133, ensure that the value that is assigned to a constant is constant as well.

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...