Why doesn’t C# use arithmetic overflow checking by default? [duplicate]

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.

Leave a Comment