checked,unchecked

时间:2022-03-01 05:08:04

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);

}

算术运算导致溢出。