views:

96

answers:

1

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 operators 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?

+6  A: 

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".
David Morton
Thanks, I never noticed that Advanced button before!
Qwertie