編譯器錯誤 CS0150
更新: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;
}
}
}