Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Compiler Error CS0031

Constant value 'value' cannot be converted to a 'type'. (use 'unchecked' syntax to override)

An attempt was made to assign a value to a variable whose type cannot store the value. For more information, see Types (C# Programming Guide).

The following sample generates CS0031 in both checked and unchecked contexts:

// CS0031.cs
namespace CS0031
{
   public class a
   {
      public static void Main()
      {
         int num = (int)2147483648M; //CS0031
         // Try using a larger numeric type instead:
         // long num = (long)2147483648M; //CS0031

         const decimal d = -10M; // Decimal literal
            unchecked
            {
                const byte b = (byte)d; // CS0031
                // For small values try using a signed byte instead:
                // const sbyte b = (sbyte)d;
            }
      }
   }
}
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft