Код: Выделить всё
int x = int.MinValue;
int y;
checked {
// Overflow. 2^31 cannot be represented in signed integer.
try { y = -x; } catch (OverflowException) { Console.WriteLine ("Overflow 1"); }
// Overflow. Though the final result (2^31-1) fit in an integer, the intermediate value does not.
try { y = -x-1; } catch (OverflowException) { Console.WriteLine ("Overflow 2"); }
// No overflow. Please explain.
try { y = -1-x; } catch (OverflowException) { Console.WriteLine ("Overflow 3"); }
}
< /code>
output: < /p>
Overflow 1
Overflow 2
Подробнее здесь: https://stackoverflow.com/questions/797 ... r-overflow