相等运算符 - 始终左结合,这重要吗?

发布于 2024-12-20 13:35:28 字数 375 浏览 2 评论 0原文

在 c# 语言规范中, 它被发现 “除了赋值运算符之外,所有二元运算符都是左结合的”

但这对于相等运算符会产生影响吗? == 和 != ?

说: bool X,Y,Z ...

return x==Y==z ... 等等?

即使返回语句中 x,y,z ... 的顺序不同,结果是否总是相同, 总是返回相同的值? (如果 x,y,z 的初始值显然相同)

(x==y)==z 等于 x==(Y==z) 并且 (x!=y)==z 等于 x !=(Y==z)

对吗?

In c# Language Specification,
its to be found
"Except for the assignment operators, all binary operators are left-associative"

But will it make at difference for Equality operators? == and != ?

Say:
bool X,Y,Z ...

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

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? (if the init values of x,y,z is the same obviously)

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

Right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冷情 2024-12-27 13:35:28

在 C# 4.0 语言规范中,可以找到“除了赋值运算符之外,所有二元运算符都是左关联的”

仅供参考,该行是一个错误。空合并运算符也是右关联的——但这并不重要。请参阅 https://stackoverflow.com/a/6269773/88656,了解 Jon 对此问题的精彩分析。

对于仅对布尔值进行操作的相等运算符 ==!= 会有区别吗?

不。正如您正确地注意到的,当仅处理相等运算符且仅处理布尔值时,运算符结合性是无关紧要的。虽然这是一个有趣的琐事,但我不确定为什么它特别相关。

许多人回答说这很重要,因为操作数可能会产生副作用。这些人错了;这是一个极其常见的错误。操作数的计算顺序与运算符的优先级或结合性无关。例如,在 C 中,允许一致的实现以任何顺序计算副作用。如果你说:

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

在 C 中,那么你可以保证 D()、E() 和 F() 不会在 A()、B() 和 C() 之前运行。但不能保证 B() 的副作用在 C() 之前计算。 A()、B() 和 C() 可以在 C 中以任何顺序合法地执行。 运算符关联性,无论是“自然的”还是由括号强加的,都不会对计算副作用的顺序施加任何限制。

这种情况在 C# 中有所改善。在 C# 中,无论运算符的优先级或关联性如何,都可以保证观察到操作数的副作用从左到右发生。也就是说,如果您有 A() + B() * C(),那么您可以保证 A() 发生在 B() 之前,B() 发生在 C() 之前,无论事实上,A() 涉及的加法的优先级低于乘法。

运算符优先级和结合性控制运算符运行的顺序,而不是操作数的计算顺序。

In the C# 4.0 Language Specification, its to be found "Except for the assignment operators, all binary operators are left-associative"

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.

Will it make at difference for equality operators == and != operating only on 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. 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(). But you are not guaranteed that side effects of B() are computed before C(). A(), B() and C() can be executed in any order in C, legally. 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#. 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. 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.

Operator precedence and associativity controls the order in which the operators run, not the order in which the operands are computed.

深爱成瘾 2024-12-27 13:35:28
bool x = false;
int y = 0;
int z = 1;

Console.WriteLine(x == y == z);//won't compile
Console.WriteLine(y == z == x);//will compile

一切都取决于 == 的关联性。

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 ==.

乞讨 2024-12-27 13:35:28

在你的例子中,这很重要。 x==y==z 表示“Z 是布尔值,如果 'x==y && z==true' 或 'x!=y,则该行将返回 true &&z==假'”。

所以是的,这很重要。

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'".

So yes, it matters.

红墙和绿瓦 2024-12-27 13:35:28

将其视为 (x ^ y ^ z),您就会得到答案。

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

鱼忆七猫命九 2024-12-27 13:35:28

结果是一样的。 ==!= 的关联性并不重要,但必须选择关联性。

The result will be the same. The associativity of == and != does not matter, but an associativity must be chosen.

哭了丶谁疼 2024-12-27 13:35:28

如果您为具有副作用的相等运算符提供重载,则可能会产生影响。显然,这是一个可怕且毫无意义的想法,但却是可能的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文