Saturday, October 11, 2008

Checked/Unchecked Arithmetic:

Checked/Unchecked Arithmetic:

When a value exceeds the limitations of a data type, it is said to overflow, this can sometime produce unexpected result.It can even be dangerous, particularly when using unsafe code. to prevent this type of Error C# provide two new operator. if such overflow condition in code ,it will not let that be happen and throw Exception,

(1)Checked Arithmetic: if over flow occurs in between below code Exception Occurs.

checked

{ int i = int.MaxValue; Console.WriteLine(i); i++; Console.WriteLine(i);

}

(2)Unchecked Arithmetic: any overflow in unchecked operator will simply truncated.

unchecked

{

int i = int.MaxValue; Console.WriteLine(i); i++; Console.WriteLine(i);

}

Fore more detail please visit.

http://www.blackwasp.co.uk/CheckedArithmetic.aspx