C# Error CS0150 – A constant value is expected

C# Compiler Error

CS0150 – A constant value is expected

Reason for the Error

You will receive this error in C# when you have used a variable instead of using a constant where it is expected to be used.

For example, try compiling the below code snippet.

namespace DeveloperPublishNamespace
{
   public class DeveloperPublish 
    {
        public static void Main()
        {
            int switchcriteria = 10;
            int CheckValue = 1;

            switch (switchcriteria)
            {
                case CheckValue:
                    break;
            }
        }
    }

}

In this example, we have used the variable “CheckValue” for the switch case which will result with the C# error code CS0150.

Error CS0150 A constant value is expected ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 12 Active

C# Error CS0150 – A constant value is expected

Solution

In the above example , use a constant value instead of variable in the switch case to fix the error.

namespace DeveloperPublishNamespace
{
   public class DeveloperPublish 
    {
        public static void Main()
        {
            int switchcriteria = 10;
            int CheckValue = 1;

            switch (switchcriteria)
            {
                case 1:
                    break;
            }
        }
    }

}

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...