Equality operators are always left-associative, will that matter?

In the C # Language Specification , it can be found "With the exception of assignment operators, all binary operators are left-associative"

But will it matter for Equality operators? == and! =?

Say: bool X, Y, Z ...

return x == Y == z ... and so on?

Will the result always be the same, even if the order of x, y, z ... is different in inverse expression, always return the same value? (if the values ​​of init x, y, z are the same, obviously)

(x == y) == z Equals x == (Y == z) and for (x! = y) == z Equals x! = (Y == z)

Right?

+6
source share
6 answers

You can find it in the C # 4.0 Language Specification "Except for assignment operators, all binary operators are left-associative"

FYI this line is a mistake. The zero-coalescing operator is also right-associative β€” no matter what it matters. See fooobar.com/questions/10383 / ... for John a great analysis of this problem.

Will it be executed with a difference for the equality operators == and != , Working only on bools?

Not. As you correctly noted, the associativity of operators does not matter when working only with equality operators and only with bools. Although this is an interesting little thing, I do not know why this is especially important.

Many people answered that it matters because operands can have side effects. These people are mistaken; This is an extremely common mistake. The order in which the operands are evaluated has nothing to do with the priority or associativity of the operators. In C, for example, the corresponding implementation allows you to calculate side effects in any order. If you say:

 x = (A() != B()) == C(); y = D() != (E() == F()); 

In C, you are guaranteed that D (), E (), and F () are not executed until A (), B (), and C (). But you are not guaranteed that the side effects of B () are calculated before C (). A (), B (), and C () can be executed in any order in C, legally. Associative associativity, "natural" or imposed by parentheses, does not impose any restrictions on the order in which side effects can be calculated.

In C #, the situation has improved a bit. In C # you are guaranteed that the side effects of the operands will be executed from left to right , regardless of the priority or associativity of the operators . That is, if you have A() + B() * C() , then you are guaranteed that A () will happen before B () and that B () will happen before C (), regardless of what A ( ) participates in an addition that has a lower priority than multiplication.

The priority and associativity of operators control the order in which the operators work, and not the order in which the operands are calculated.

+13
source
 bool x = false; int y = 0; int z = 1; Console.WriteLine(x == y == z);//won't compile Console.WriteLine(y == z == x);//will compile 

All the way to associativity == .

+8
source

In your example, this will make a difference. x==y==z means that "Z is a bool, and the string will return true if" x == y && z == true "or if" x! = y && z == false ".

So yes, this is important.

+5
source

Think of it as (x ^ y ^ z) and you will have your answer.

+1
source

The result will be the same. Associativity == and != Does not matter, but associativity must be chosen.

0
source

This could potentially make a difference if you provided an overload for an equality operator that had side effects. A terrible and meaningless idea, obviously, but possible.

0
source

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


All Articles