Compiler Error CS0151

应输入整型值

在需要整型数据类型的情况中使用了变量。有关更多信息,请参见类型(C# 编程指南)

当没有转换或者可用的隐式转换导致了不明确的情形时,会出现此错误。下面的示例生成 CS0151。

// CS0151.cs
public class MyClass
{
   public static implicit operator int (MyClass aa)
   {
      return 0;
   }

   public static implicit operator long (MyClass aa)
   {
      return 0;
   }

   public static void Main()
   {
      MyClass a = new MyClass();

      // Compiler cannot choose between int and long
      switch (a)   // CS0151
      // try the following line instead
      // switch ((int)a)
      {
         case 1:
            break;
      }
   }
}

在 Visual Studio 2008 及更高版本中,void 方法调用会生成 CS0151。通过调用返回整数类型(如 intlong)的方法可以修复该错误。

class C
{
    static void Main()
    {

        switch (M()) // CS0151
        {
            default:
                break;
        }
    }

    static void M()
    {
    }
}