更新:2007 年 11 月
错误消息
应输入常数值
在需要常数的地方发现了变量。有关更多信息,请参见 switch(C# 参考)。
下面的示例生成 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 1:
}
}
}
}
如果以变量值指定数组大小并以数组初始值设定项初始化数组,也会生成此错误。若要移除该错误,请以单独的语句初始化数组。
// 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;
}
}
}