The BODMAS principle in .NET.

Today I discovered that the .NET platform follows the order of the BODMAS operations when performing the calculation. That is, the calculations are performed in the following order:

  • Brackets
  • Orders
  • The Department
  • Multiplication
  • Adding
  • Subtraction

However, I searched and cannot find documentation confirming that .NET definitely follows this principle, are there any such documents anywhere? I would appreciate it if you could point me in the right direction.

+5
source share
3 answers

Please note that C # does not comply with the BODMAS rule as you went to school. Suppose you have:

A().x = B() + C() * D();

You can naively believe that multiplication is done “first”, then addition, and assignment last, and therefore this is equivalent:

c = C();
d = D();
product = c * d;
b = B();
sum = b + product;
a = A();
a.x = sum;

, . BODMAS , ; .

# . , , :

a = A();
b = B();
c = C();
d = D();
product = c * d;
sum = b + product;
a.x = sum;

, # . :

A().x = B() + C() + D() * E();

:

a = A();
b = B();
c = C();
sum1 = b + c;
d = D();
e = E();
product = d * e;
sum2 = sum1 + product;
a.x = sum2;

., ; .

, " , , ". , :

A().x = ( ( B() + C() ) + ( D() * E() ) );

. - , , .

, . :

http://blogs.msdn.com/b/ericlippert/archive/tags/precedence/

+23

http://msdn.microsoft.com/en-us/library/aa691323%28v=vs.71%29.aspx details all the priorities and doesn't exactly match your list.

+1
source

All Articles