byte b;
int i;
unchecked
{
b = 255 + 255; //overflows
i = 100 + int.MaxValue+100; // works
}
1) Is the reason why b expression (b = 255 + 255;) causes an overflow error due to being affected by two conflicting rules, where first rule R1 states:
A constant-expression (§7.19) of type
int can be converted to type sbyte,
byte, short, ushort, uint, or ulong,
provided the value of the
constant-expression is within the
range of the destination type.
while second rule R2 states that within unchecked context overflow is allowed.
And in the case of b expression, R1 takes precedence over R2, and thus since constant expression 255+ 255 is not within the range of destination type ( which is byte ), R1 causes an error, even though R2 permits an overflow?
2)
a) Here’s my reasoning as to why i expression (i = 100 + int.MaxValue+100;) doesn’t cause an error:
1 – When compiler starts computing i expression, it doesn’t try to promote values 100 and int.MaxValue to type long before performing the addition ( thus during the computation process the two values are still of type int )
2 – the addition does cause an overflow, but since this happens within unchecked context, no error is thrown
3 – Since the two values didn’t get promoted to long, the resulting value is also of type int and as such the resulting value is within the range of destination type
b) But if instead compiler did promote 100 and int.MaxValue; to type long before performing the addition, then i expression would cause an error due to violation of rule R1?!
thanx
Yes. The constants are all
intby default, so the second statement will overflow happily and stay within int. You can see that it fails if you make one of the constants long: