Tutorial by Examples

There is a maximum capacity an integer can store. And when you go over that limit, it will loop back to the negative side. For int, it is 2147483647 int x = int.MaxValue; //MaxValue is 2147483647 x = unchecked(x + 1); //make operation explicitly unchecked so that the ...
Overflow also happens during the operation. In the following example, x is an int, 1 is an int by default. Therefore addition is an int addition. And the result will be an int. And it will overflow. int x = int.MaxValue; //MaxValue is 2147483647 long y = x + 1; //...
There is overflow in the following code int x = int.MaxValue; Console.WriteLine(x + x + 1L); //prints -1 Whereas in the following code there is no overflow int x = int.MaxValue; Console.WriteLine(x + 1L + x); //prints 4294967295 This is due to the left-to-right ordering of the operations...

Page 1 of 1