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

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;
Console.WriteLine(result);
Console.ReadLine();

image

Leave a Reply