Checked and Unchecked

suggest change

C# statements execute in either a checked or an unchecked context. In a checked context, arithmetic overflow raises an exception. In an unchecked context, arithmetic overflow wraps: the extra bits are discarded.

short m = 32767;   
short n = 32767;
int result1 =  checked((short)(m + n));   //will throw an OverflowException
int result2 =  unchecked((short)(m + n)); // will return -2

If neither context is specified, the default depends on other factors, such as compiler options. Constant expressions are checked by default.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents