23

Possible Duplicate:
Why don’t languages raise errors on integer overflow by default?

Why doesn't C# use arithmetic overflow checking by default?

I figure that it would generally be better to have exceptions occur when this occurs so that errors aren't obscured. I know that it's occasionally useful to take advantage of the 'wrapping' behaviour that occurs, but the unchecked keyword could be used in these circumstances to make the intentions explicit.

I expect that this decision was made intentionally, perhaps to increase compatibility with other C-based languages.

2
  • 5
    Probably for performance reasons - overflow checking is slow, and not needed in the vast majority of cases. Commented Nov 6, 2012 at 21:08
  • @MichaelPetito, thanks; that question seems better than this one, and it has good answers, so I think this should be closed. Commented Nov 6, 2012 at 21:53

2 Answers 2

26

The C# Language Specification says this:

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.

The reason for this choice is probably performance. I agree that this decision leads to errors among those who are not aware of "silent" integer overflow.

If your C# files belong to a C# project file (*.csproj), then that file holds configuration of the "default" overflow checking context. To changed it, see To set this compiler option in the Visual Studio development environment in this page.

If you don't use .csproj files, you're probably compiling everything from the command line, and then the above page tells you what command line option to use to set the default overflow checking context.

Sign up to request clarification or add additional context in comments.

Comments

2

See my answer to similar question here: Best way to handle Integer overflow in C#?

... there is a C# compiler option that defines how expressions outside of checked and unchecked are handled: /checked.

The default behavior is suitable for most applications. For other applications, where strict checking should be the default, there is a compiler option to enable such behavior.

16 Comments

Thanks for taking the time to answer, but I asked why rather than how.
@H2CO3 No, if I was designing a language, I'd default to wrap-around on overflow as well. Why? Because the performance impact of overflow checking is really that extreme.
I'm not speaking for the downvote, I'm just saying that your implication that MS did it because they felt like it isn't correct.
@MichaelPetito, if you didn't intend to answer tha actual question, I think it would be more suitable to instead post a comment on the question.
@supercat The problem is that there's no way to efficiently implement that thread-static flag. On x86 at least, there is a flag register which is set on an overflow. But that gets overwritten by pretty much all operations. So you'd need to fetch and save that flag bit after most instructions - in which case, you've just added back all that overhead which you're trying to avoid. The only way checked arithmetic will be efficient is if it's implemented in the hardware. But that isn't the case with any mainstream processors.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.