Using Unchecked operator in mathematical
operation:
class
Listing4_5
{
public
static void
Main()
{
int
Int1;
int
Int2;
int
Int1PlusInt2;
Int1 = 2000000000;
Int2 = 2000000000;
Int1PlusInt2 = Int1 + Int2;
System.Console.WriteLine(Int1PlusInt2);
}
}
The above code is run in program is overflow.
Using checked operator in mathematical operation:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication3
{
class Program
{
static void
Main(string[] args)
{
try
{
int Int1;
int Int2;
int Int1PlusInt2;
Int1 =
2000000000;
Int2 =
2000000000;
Int1PlusInt2
= checked(Int1 + Int2);
}
catch (Exception
ex)
{
System.Console.WriteLine(ex);
Console.ReadLine();
}
}
}
}
The above code is using in checked operator so its prevent overflow.