Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

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;
            }
        }
    }

}