Arithmetic Overflow for Constant Expressions evaluated at Compile time in C#

Your 2020 Plan. Learn 20 minutes a day for 20 days. Online courses from €9.99.

The checked operator or block in C# can be used to invoke the overflow exception instead of silently skipping the overflow exception . The developers could also use the /checked compiler switch to achieve the same .

How are the constant expressions evaluated for the Arithmetic Overflow ?

Even if the checked operator , check block or /checked compiler switch is NOT enabled , the constant expressions are evaluated at the compile time in C# unless the unchecked operator is applied.

For example , the below code snippet would show the compiler error

“The operation overflows at compile time in checked mode”

int result = int.MaxValue + 1;


Leave a Reply

Your email address will not be published. Required fields are marked *