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

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

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...