C# 中的 == 与等于

发布于 2024-09-26 03:47:12 字数 287 浏览 3 评论 0原文

C# 中 == 和 Equals 的计算有什么区别?

对于前,

if(x==x++)//Always returns true

if(x.Equals(x++))//Always returns false 

已编辑:

     int x=0;
     int y=0;

     if(x.Equals(y++))// Returns True

What is the difference between the evaluation of == and Equals in C#?

For Ex,

if(x==x++)//Always returns true

but

if(x.Equals(x++))//Always returns false 

Edited:

     int x=0;
     int y=0;

     if(x.Equals(y++))// Returns True

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

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

发布评论

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

评论(2

捂风挽笑 2024-10-03 03:47:12

根据规范,这是预期的行为。

第一个的行为受规范第 7.3 节的约束:

表达式中的操作数是从左到右计算的。例如,在F(i) + G(i++) * H(i)中,使用i的旧值调用方法F,然后使用i的旧值调用方法G,并且,最后,使用 i 的新值调用方法 H。这与运算符优先级分开且无关。

因此,在 x==x++ 中,首先计算左操作数 (0),然后计算右操作数 (0, >x 变为 1),然后比较完成:0 == 0 为 true。

第二个的行为受第 7.5.5 节管辖:

  • 如果 M 是在值类型中声明的实例函数成员:
    • E 被评估。如果此评估导致异常,则不会执行进一步的步骤。
    • 如果 E 未被分类为变量,则会创建 E 类型的临时局部变量,并将 E 的值赋给该变量。然后 E 被重新分类为对该临时局部变量的引用。临时变量在 M 中可以这样访问,但不能以任何其他方式访问。因此,只有当E是一个真正的变量时,调用者才有可能观察到M对其所做的改变。
    • 参数列表的计算方式如第 7.5.1 节中所述。
    • M 被调用。 E 引用的变量成为 this 引用的变量。

请注意,值类型通过引用传递到它们自己的方法。

因此,在 x.Equals(x++) 中,首先评估目标(E 是变量 x),然后评估参数(0,x 变为 1),然后比较完成:x.Equals(0) 为 false。

编辑:我还想赞扬 dtb 现已撤回的评论,该评论是在问题关闭时发布的。我认为他也说了同样的话,但由于评论长度的限制,他无法完全表达出来。

According to the specification, this is expected behavior.

The behavior of the first is governed by section 7.3 of the spec:

Operands in an expression are evaluated from left to right. For example, in F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old value of i, and, finally, method H is called with the new value of i. This is separate from and unrelated to operator precedence.

Thus in x==x++, first the left operand is evaluated (0), then the right-hand is evaluated (0, x becomes 1), then the comparison is done: 0 == 0 is true.

The behavior of the second is governed by section 7.5.5:

  • If M is an instance function member declared in a value-type:
    • E is evaluated. If this evaluation causes an exception, then no further steps are executed.
    • If E is not classified as a variable, then a temporary local variable of E’s type is created and the value of E is assigned to that variable. E is then reclassified as a reference to that temporary local variable. The temporary variable is accessible as this within M, but not in any other way. Thus, only when E is a true variable is it possible for the caller to observe the changes that M makes to this.
    • The argument list is evaluated as described in §7.5.1.
    • M is invoked. The variable referenced by E becomes the variable referenced by this.

Note that value types are passed by reference to their own methods.

Thus in x.Equals(x++), first the target is evaluated (E is x, a variable), then the arguments are evaluated (0, x becomes 1), then the comparison is done: x.Equals(0) is false.

EDIT: I also wanted to give credit to dtb's now-retracted comment, posted while the question was closed. I think he was saying the same thing, but with the length limitation on comments he wasn't able to express it fully.

把回忆走一遍 2024-10-03 03:47:12

评估顺序。 ++ 首先评估(第二个示例)。但在第一个示例中,== 首先执行。

Order of evaluation. ++ evaluates first (second example). But in the first example, == executes first.

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