Compiler respond differently when assigning int.MaxValue to variable

Viewed 124

According to the documentation int.MaxValue is an int field. When I do

int a = int.MaxValue;
int b = a + 1;

b has the value of int.MinValue as expected. But when I do

int a = int.MaxValue + 1;

I get compilation error

The operation overflows at compile time in checked mode

Why is the there a difference?

3 Answers
Related