Integer overflow

suggest change

There is a maximum value an int can store: 2147483647. In an unchecked context, going past that limit wraps to the negative side. A checked context throws OverflowException instead.

int x = int.MaxValue;                //MaxValue is 2147483647
x = unchecked(x + 1);                //make operation explicitly unchecked so that the example also works when the check for arithmetic overflow/underflow is enabled in the project settings 
Console.WriteLine(x);                //Will print -2147483648
Console.WriteLine(int.MinValue);     //Same as Min value

For values outside this range, use System.Numerics.BigInteger. See https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents