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.
- Double click the Properties folder.
- Build tab.
- Click the Advanced... button.
- Uncheck "Check for arithmetic overflow/underflow".
精彩评论