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

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