Simple code indeed :
int number = int.MaxValue;
number = number+1;
Console.WriteLine(number);
Questions :
1) there’s should be overflowException. there isn’t. why is that?
2) Does the number -2147483648 indicates something about :
-
) how many bytes is beyond limit size ?
-
) if i see
-2147483648, How can I know ( if there wasnt an exception) – if this number came from exception ? – must I wrap it with Try Catch ?
p.s. when i wrote :
int number = int.MinValue;
number = number-1;
Console.WriteLine(--number);
I got : 2147483647 (still no exception).
By default, C# doesn’t do overflow checking. You can enable it for a whole assembly (in the “Advanced” bit of the build tab in VS, or
/checked+from the command line) or for a block/expression using thecheckedkeywordFor the second part of your question,
-2147483648is justint.MinValue– signed integers do that, rolling from a very large positive number to a very large negative number.For example, if you use
sbyte(the signed byte type):There’s nothing funny going on here – it’s just normal 2s complement overflow.
Or using
byte(unsigned)