Why int.MaxValue - int.MinValue = -1?

c#

Solution

`int.MaxValue - int.MinValue` = a value which int cannot hold. Thus, the number wraps around back to -1.

It is like 2147483647-(-2147483648) = 4294967295 which is not an int

Int32.MinValue Field

The value of this constant is -2,147,483,648; that is, hexadecimal 0x80000000.

And Int32.MaxValue Field

The value of this constant is 2,147,483,647; that is, hexadecimal 0x7FFFFFFF.

From MSDN

When integer overflow occurs, what happens depends on the execution context, which can be checked or unchecked. In a checked context, an OverflowException is thrown. In an unchecked context, the most significant bits of the result are discarded and execution continues. Thus, C# gives you the choice of handling or ignoring overflow.

Problem

To my understanding, that should give you an overflow error and when I write it like this: ``` public static void Main() { Console.WriteLine(int.MaxValue - int.MinValue); } ``` it does correctly give me an overflow error. However: ``` public static void Main() { Console.WriteLine(test()); } public static Int32 test(int minimum = int.MinValue, int maximum = int.MaxValue) { return maximum - minimum; } ``` will output -1 Why does it do this? It should throw an error because its clearly an overflow!

Original source