Compiler Error CS0150
A constant value is expected
A variable was found where a constant was expected. For more information, see switch.
The following sample generates CS0150:
// CS0150.cs
namespace MyNamespace
{
public class MyClass
{
public static void Main()
{
int i = 0;
int j = 0;
switch(i)
{
case j: // CS0150, j is a variable int, not a constant int
// try the following line instead
// case 0:
}
}
}
}
This error is also produced when an array size is specified with a variable value and initialized with an array initializer. To remove the error, initialize the array in a separate statement or statements.
// CS0150.cs
namespace MyNamespace
{
public class MyClass
{
public static void Main()
{
int size = 2;
double[] nums = new double[size] { 46.9, 89.4 }; //CS0150
// Try the following lines instead
// double[] nums = new double[size];
// nums[0] = 46.9;
// nums[1] = 89.4;
}
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.