Integer overflow exception – WhereIsMyAnswer
Why I get a compiler error here:
int a = 2147483647 + 10;
and not here, if I’m performing the same operation:
int ten = 10; int b = 2147483647 + ten;
I’m learning the usage of checked and the MSDN web site doesn’t clear why the OverflowException is raised in the first code snippet:
By default, an expression that contains only constant values causes a
compiler error if the expression produces a value that is outside the
range of the destination type. If the expression contains one or more
non-constant values, the compiler does not detect the overflow.
It only explains the behavior but not the reasons for that behavior. I’d like to know what happens under the hood.
Read more here: Source link