Consider this
int i = 2147483647;
var n = i + 3;
i = n;
Console.WriteLine(i); // prints -2147483646 (1)
Console.WriteLine(n); // prints -2147483646 (2)
Console.WriteLine(n.GetType()); // prints System.Int32 (3)
I am confused with following
- (1) how could int hold the value -2147483646 ? (int range = -2,147,483,648 to 2,147,483,647)
- (2) why does this print -2147483648 but not 2147483648 (compiler should decide better type as int range exceeds)
- (3) if it is converted somewhere, why n.GetType() gives System.Int32 ?
Edit1: Made the correction: Now you will get What I am Getting. (sorry for that)
var n = i + 1; to
var n = i + 3;
Edit2: One more thing, if it as overflow, why is an exception not raised ?
Addition: as the overflow occurs, is it not right to set the type for
var n
in statement var n = i + 3;
to another type accordingly ?
you are welcome to suggest a better title, as this is not making sense to.... me at least
Thanks