简体   繁体   English

平等运营商 - 总是左联合的,是否重要?

[英]Equality operators - always left-associative, will it matter?

In c# Language Specification , its to be found "Except for the assignment operators, all binary operators are left-associative" 在c# 语言规范中 ,可以找到“除了赋值运算符,所有二元运算符都是左关联的”

But will it make at difference for Equality operators? 但它会为平等运营商带来不同吗? == and != ? ==和!=?

Say: bool X,Y,Z ... 说:bool X,Y,Z ......

return x==Y==z ... and so on? return x == Y == z ...依此类推?

Wont the result always be the same even if the order of x,y,z ... differs in the return statement, always return the same value? 即使x,y,z ...的顺序在return语句中有所不同,结果总是相同的,总是返回相同的值吗? (if the init values of x,y,z is the same obviously) (如果x,y,z的init值明显相同)

(x==y)==z Equals to x==(Y==z) and for (x!=y)==z Equals to x!=(Y==z) (x == y)== z等于x ==(Y == z)和(x!= y)== z等于x!=(Y == z)

Right? 对?

In the C# 4.0 Language Specification, its to be found "Except for the assignment operators, all binary operators are left-associative" 在C#4.0语言规范中,它被发现“除了赋值运算符,所有二元运算符都是左关联的”

FYI that line is an error. 仅供参考该行是错误的。 The null coalescing operator is also right-associative -- not that it matters really. 空合并运算符也是右关联的 - 并不是真正重要的。 See https://stackoverflow.com/a/6269773/88656 for Jon's great analysis of this issue. 有关Jon对此问题的深入分析,请参阅https://stackoverflow.com/a/6269773/88656

Will it make at difference for equality operators == and != operating only on bools? 是否会使等式运算符==!=仅在bools上运行?

No. As you correctly note, operator associativity is irrelevant when dealing only with equality operators and only on bools. 不。正如您所正确注意的那样,只处理相等运算符时,运算符关联性是无关紧要的。 Though that is an interesting bit of trivia, I'm not sure why it is particularly relevant. 虽然这是一个有趣的琐事,但我不确定为什么它特别相关。

A number of people have answered that it makes a difference because there could be side effects in the operands. 很多人都回答说它有所不同,因为操作数可能会产生副作用。 These people are wrong; 这些人错了; this is an extremely common error. 这是一个非常常见的错误。 The order in which operands are evaluated has nothing whatsoever to do with the precedence or associativity of the operators. 评估操作数的顺序与运算符的优先级或关联性无关。 In C, for example, a conforming implementation is allowed to compute side effects in any order. 例如,在C中,允许符合要求的实现以任何顺序计算副作用。 If you say: 如果你说:

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

In C then you are guaranteed that D(), E() and F() do not run before A(), B() and C(). 在C中,您可以保证D(),E()和F()不会在A(),B()和C()之前运行。 But you are not guaranteed that side effects of B() are computed before C(). 但是你无法保证在C()之前计算B()的副作用。 A(), B() and C() can be executed in any order in C, legally. A(),B()和C()可以在C中以任何顺序执行,合法地执行。 Operator associativity, whether 'natural' or imposed by parentheses, does not impose any restriction on the order in which side effects may be computed. 操作员关联性,无论是“自然的”还是由括号强加的,都不会对可能计算副作用的顺序施加任何限制。

The situation is somewhat improved in C#. C#的情况有所改善。 In C# you are guaranteed that side effects of operands are observed to happen left to right regardless of the precedence or associativity of the operators . 在C#中,无论运算符的优先级或关联性如何,都可以保证操作数的副作用从左到右发生。 That is, if you have A() + B() * C() then you are guaranteed that A() happens before B() and that B() happens before C(), regardless of the fact that A() is involved in an addition which is lower precedence than a multiplication. 也就是说,如果你有A() + B() * C()那么你可以保证A()在B()之前发生,B()在C()之前发生,不管A()是什么参与加法,其优先级低于乘法。

Operator precedence and associativity controls the order in which the operators run, not the order in which the operands are computed. 运算符优先级和结合对照,其中运营商运行的顺序,而不是在其操作数计算的顺序。

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 down to the associativity of == . 一切都归结为==的相关性。

In your example, it would matter. 在你的例子中,它很重要。 x==y==z would mean "Z is a bool, and the line will return true if 'x==y && z==true' or if 'x!=y && z==false'". x==y==z意味着“Z是一个bool,如果'x == y && z == true'或者'x!= y && z == false'”,该行将返回true。

So yes, it matters. 所以是的,这很重要。

把它想象为(x ^ y ^ z),你就会有答案。

The result will be the same. 结果将是相同的。 The associativity of == and != does not matter, but an associativity must be chosen. ==!=的关联性无关紧要,但必须选择关联性。

It could potentially make a difference if you provided an overload for the equality operator that had side effects. 如果您为具有副作用的相等运算符提供了重载,则可能会产生影响。 A terrible and pointless idea, obviously, but possible. 一个可怕而毫无意义的想法,显然,但可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM