== VS等于C#

扫码查看
本文介绍了== VS等于C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么==评价之间的差异,在C#等于?



有关防爆,

 如果(X == X ++)//始终返回true,

 如果(x.Equals(X ++))//始终返回false 

编辑:

  INT X = 0; 
INT Y = 0;

如果(x.Equals(Y ++))//返回True


解决方案

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



第一次的行为是规范的7.3节管辖。




操作数从左至右进行评价。例如,在 F(I)+ G(我+ +)* H(I),方法F使用我的旧值被调用,那么方法G称为与我的旧值,并且,最后,方法H称为与i的新值。在这是独立且无运算符优先级。




因此​​Y == X ++ ,第一左操作数进行评估( 0 ),然后右手被评估( 0 X 变成 1 ),则比较完成: 0 == 0 是真的



第二的行为是由部分7.5.5决定:



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:

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.

这篇关于== VS等于C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 17:09
查看更多