Why does the compiler not show an error when we try to split the variable by zero

If we try to run this code:

int d = 10/0; 

We get a compiler error. Therefore, we cannot divide by zero.

Now consider this code:

  int d = 10; int o = d / 0; 

d can have anything and divide anything by zero is not normal

We are not getting a compiler error. A variable has something that we cannot divide by zero. Why does the compiler not give an error when we try to split the variable by zero?

+4
source share
5 answers

You want to take a look at the C # language specification, chapter 7.18. It talks about constant expressions. I just summarize the basics.

The C # compiler is trying to try to evaluate the expression at compile time. As long as all operands have a known value and the operators are simple, then the compiler can calculate the value of the expression at compile time and directly use the result, rather than generate code to evaluate the expression at run time. This is similar to optimization, but it is not so, constant expressions are required in a number of places. Like the value of the case statement , declarations using the const keyword, enum declaration values, and [attribute] argument arguments.

So no problem with 10 / 0 , this is an expression with literal values ​​and a simple operator so that the compiler can directly calculate the result and see that it throws an exception, so it complains about it at compile time.

d / 0 not a constant expression due to the variable d . You could argue that the compiler could know the value of d , since it got the assignment in the above expression. But this is not so; optimizing such code is the work of the jitter optimizer.

+7
source

Because your variable d not constant.

The compiler performs only basic (let them say, trivial) mathematical checks. You are not thorough enough for the compiler because it uses a variable that is not const .

+7
source

In the first case, you have two constants, so the compiler resolves this at compile time, thereby detecting an error.

In the second case, since you have a variable non const (d), the compiler does not allow this at compile time, since it is not able to provide the value of d and thus will not detect an error. This will be evaluated at runtime, where it will exit out of service.

+3
source

First:

 int d=10/0; 

will be allowed due to compilation time as it is constant. The second will not be allowed, because the compiler performs only syntax checks, not a math check.

+2
source

the C # compiler only performs arithmetic checking of constant values, and where it can be said that you cannot do 10/0 . it is much more than you think for the compiler.

even more: the C # compiler allows you to:

 1.0 / 0 // Infinity 

because:

Arithmetic floating point overflow or division by zero never throws an exception, since floating point types are based on IEEE 754 and therefore have provisions for representing infinity and NaN (not numbers).

+1
source

Source: https://habr.com/ru/post/1500013/


All Articles