开发者

C# - What could cause overflow checking here?

I am accustomed to C# not performing overflow checks, as the language spec states (§7.5.12):

For non-constant expressions (expressions that are evaluated at run-time) that are not enclosed by any checked or unchecked op开发者_JAVA技巧erators or statements, the default overflow checking context is unchecked unless external factors (such as compiler switches and execution environment configuration) call for checked evaluation.

I took advantage of this when doing an array bounds check in low-level code:

if ((uint)index >= (uint)TotalCount)
    ...

If index is negative, I expect it to become a large positive number so that it exceeds TotalCount. However, to my surprise, a negative number produces OverflowException, and I have to wrap the expression in unchecked(). I looked through the project options in Visual Studio and I do not see an option to enable or disable overflow checking. So why might it be on here?


It should be in the project.

  1. Double click the Properties folder.
  2. Build tab.
  3. Click the Advanced... button.
  4. Uncheck "Check for arithmetic overflow/underflow".
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜