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

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...