如果我将 Int32.MinValue 和 -1 传递到Divide()方法中,System.OverflowException尽管块发生在unchecked块中,我还是得到 a 。
private static int Divide(int n, int d)
{
return unchecked (n / d);
}
Run Code Online (Sandbox Code Playgroud)
这让我感到惊讶 - 除非我错误地阅读了已检查/未检查的文档,否则我希望它只会给我一个溢出的输出(因为 Int32.MinValue / -1 = 2^31 = Int32.MaxValue + 1,我期待溢出到 Int32.MinValue 的值)。相反,它抛出了一个OverflowException.
这是一个显示问题的DotNetFiddle。