简体   繁体   English

== vs C等于#

[英]== vs Equals in C#

What is the difference between the evaluation of == and Equals in C#? 在C#中评估==和Equals有什么区别?

For Ex, 对于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

According to the specification, this is expected behavior. 根据规范,这是预期的行为。

The behavior of the first is governed by section 7.3 of the spec: 第一部分的行为受规范第7.3节的约束:

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. 例如,在F(i) + G(i++) * H(i) ,使用旧值i调用方法F,然后使用旧值i调用方法G,最后调用方法H与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. 因此,在x==x++ ,首先评估左操作数( 0 ),然后评估右手( 0x变为1 ),然后进行比较: 0 == 0为真。

The behavior of the second is governed by section 7.5.5: 第二部分的行为受第7.5.5节的约束:

  • If M is an instance function member declared in a value-type : 如果M是在value-type中声明的实例函数成员:
    • E is evaluated. E被评估。 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未被归类为变量,则创建E类型的临时局部变量,并将E的值分配给该变量。 E is then reclassified as a reference to that temporary local variable. 然后将E重新分类为对该临时局部变量的引用。 The temporary variable is accessible as this within M, but not in any other way. 临时变量在M中可以访问,但不能以任何其他方式访问。 Thus, only when E is a true variable is it possible for the caller to observe the changes that M makes to this. 因此,只有当E是真变量时,呼叫者才有可能观察到M对此做出的变化。
    • The argument list is evaluated as described in §7.5.1. 参数列表按照§7.5.1中的描述进行评估。
    • M is invoked. M被调用。 The variable referenced by E becomes the variable referenced by this. E引用的变量成为由此引用的变量。

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. 因此,在x.Equals(x++) ,首先计算目标(E是x ,变量),然后计算参数( 0x变为1 ),然后进行比较: x.Equals(0)为false 。

EDIT: I also wanted to give credit to dtb's now-retracted comment, posted while the question was closed. 编辑:我也想赞扬dtb现在收回的评论,在问题结束时发布。 I think he was saying the same thing, but with the length limitation on comments he wasn't able to express it fully. 我认为他说的是同样的事情,但由于评论的长度限制,他无法完全表达。

Order of evaluation. 评估顺序。 ++ evaluates first (second example). ++首先评估(第二个例子)。 But in the first example, == executes first. 但在第一个例子中,==首先执行。

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

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