编译器错误 CS0150

应输入常量值

在应发现常量的位置处发现变量。 有关更多信息,请参见 switch

以下示例生成 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:  
         }  
      }  
   }  
}  

当数组大小由变量值指定并由数组初始值设定项初始化时,也会产生此错误。 若要删除该错误,在单独语句或多个语句中初始化此数组。

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