編譯器錯誤 CS0031
更新:2007 年 11 月
錯誤訊息
無法將常數值 'value' 轉換為 'type' (可使用 'unchecked' 語法修正)
嘗試指定一個值給其型別無法儲存該值的變數。如需詳細資訊,請參閱資料型別 (C# 程式設計手冊)。
在檢查或不檢查的內容中,下列範例都會產生 CS0031:
// 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;
}
}
}
}