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
Это ошибка при проверке переполнения C# или что -то, что связано с архитектурой процессора?
Почему C# проверено Ключевое слово не рассматривает -1 -int.minvalue как переполнение?[code]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 [/code] Это ошибка при проверке переполнения C# или что -то, что связано с архитектурой процессора?