checked,unchecked

static void Main(string[] args)
{
byte b1 = 100;
byte b2 = 250;
//Checked
try
{
byte sum = checked ((byte) Add(b1, b2));
Console.WriteLine(sum);
Console.ReadLine();
}
catch (OverflowException Ex)
{
Console.WriteLine(Ex.Message);
}
}

static int Add(int x, int y)
{
return x + y;
}

 

unchecked

{

byte sum=(byte)(100+200);

Console.WriteLine(sum);

}

 

算术运算导致溢出。

posted @ 2016-06-03 23:10  FredGrit  阅读(138)  评论(0编辑  收藏  举报