Checked and Unchecked
suggest changeC# 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents