Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Compiler Error CS0151
Collapse the table of content
Expand the table of content

Compiler Error CS0151

A value of an integral type expected

A variable was used in a situation where an integral data type was required. For more information, see Types (C# Programming Guide).

This error can occur when there is no conversion or if the available implicit conversions result in an ambiguous situation. The following sample generates 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;
      }
   }
}

In Visual Studio 2008 and later, a void method invocation generates CS0151. You can fix the error by calling a method that returns an integral type such as int or long.

class C
{
    static void Main()
    {

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

    static void M()
    {
    }
}
Show:
© 2015 Microsoft