编译器错误 CS0031Compiler Error CS0031

常量值“value”无法转换为“'type”。Constant value 'value' cannot be converted to a 'type'.

尝试对其类型不能存储值的变量进行复制。An attempt was made to assign a value to a variable whose type cannot store the value. 有关详细信息,请参阅类型For more information, see Types.

在已选中和未选中的上下文中,下面的示例都将生成 CS0031:The following sample generates CS0031 in both checked and unchecked contexts:

// CS0031.cs
namespace CS0031
{
    public class Program
    {
        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;
            }
        }
    }
}

请参阅See also