编译器错误 CS0220
更新:2007 年 11 月
错误消息
在检查模式下,运算在编译时溢出
checked(这是默认设置)检测到某个运算并导致了数据丢失。为解决该错误,请更正赋值输入或使用 unchecked。有关更多信息,请参见Checked 和 Unchecked(C# 参考)。
下面的示例生成 CS0220:
// CS0220.cs
using System;
class TestClass
{
const int x = 1000000;
const int y = 1000000;
public int MethodCh()
{
int z = (x * y); // CS0220
return z;
}
public int MethodUnCh()
{
unchecked
{
int z = (x * y);
return z;
}
}
public static void Main()
{
TestClass myObject = new TestClass();
Console.WriteLine("Checked : {0}", myObject.MethodCh());
Console.WriteLine("Unchecked: {0}", myObject.MethodUnCh());
}
}