c# checked and unchecked blocks

Categories: Programming
Tags:
Comments: No Comments
Published on: June 6, 2010

You can use checked and unchecked blocks to throw or suppress exceptions while converting between data types.

checked{

int max = int.MaxValue;

max = max + 1;

Console.WriteLine(max);

}

Above code will throw an exception, while the below code surpress the exception but prints an overflowed value.

unchecked{

int max = int.MaxValue;
max = max + 1;

Console.WriteLine(max);

}

This will print a funny number, a negative number. But doesnt throw an exception.

Share this

Welcome , today is Tuesday, February 7, 2012

Bad Behavior has blocked 251 access attempts in the last 7 days.