When I execute the following code, I have (for me) some unexpected behaviour.
int i = Int32.MinValue;
i--;
if (i == Int32.MaxValue)
{
Console.WriteLine("i == Int32.MaxValue");
}
i++;
if (i == Int32.MinValue)
{
Console.WriteLine("i == Int32.MinValue");
}
Why doesn’t the -1 on Int32.MinValue throw an Exception?
Because of the underflow the values wrap around.
You need to use a
checkedsection if you want overflows/underflows to throw.