Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 237
Sam Avatar asked Nov 06 '12 21:11

Sam


People also ask

Why there is no string in C?

There is no string type in C . You have to use char arrays. By the way your code will not work ,because the size of the array should allow for the whole array to fit in plus one additional zero terminating character.

Why is C not A or B?

Because C comes after B The reason why the language was named “C” by its creator was that it came after B language. Back then, Bell Labs already had a programming language called “B” at their disposal.

What does C++ have that C does not?

C++ was developed by Bjarne Stroustrup in 1979. C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming. C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language.

Is C very difficult?

It is not hard to learn C. Just like any other skill, you will need patience and resilience to master coding using C. The programming language features 32 keywords for its syntax. This makes it a relatively simple coding language to learn.


2 Answers

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.

like image 122
Jeppe Stig Nielsen Avatar answered Oct 21 '22 10:10

Jeppe Stig Nielsen


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.

like image 2
Michael Petito Avatar answered Oct 21 '22 11:10

Michael Petito