问题描述
object a = "1";
object b = "1";
Console.WriteLine(a == b); // returns True
object c = 1;
object d = 1;
Console.WriteLine(c == d); // returns False
上面的代码返回整数和字符串不同的结果。我不是一直搞不明白这是为什么。是否有人可以帮助我了解这背后的原因是什么?
The above code returns different results for integer and string. I am not been able to understand why. Can someone please help me to understand the reason behind this?
什么是 ==
(运营商之间的差异)和的ReferenceEquals
(功能)?
And what is the difference between ==
(operator) and ReferenceEquals
(function)?
推荐答案
虽然的了 ==
检查引用equality.Just添加点链接也说了同样的事情。
Although Ed S has answered the point that ==
checks reference equality.Just to add the MSDN link which also says the same thing
有关预定义的值类型,等号(= =)返回true,如果
操作数的值相等,否则为false。对于字符串以外参考
类型,==如果两个操作数是指
相同的对象返回true。对于字符串类型,==比较
字符串的值。
如果你正在寻找那么比较对象,你因为你问的==和的ReferenceEquals之间的区别,那么你应该注意可以使用等于
方法。
If you are looking to compare objects then you may use the Equals
method.
另外该 ==
是超载和等于
是压倒一切的。
Also as you asked the difference between the == and referenceEquals then you should note that ==
is overloading and Equals
is overriding .
所以,如果你说
string x = "ABCD";
string y = 'A' + "BCD"; // ensure it's a different reference
if (x == y) { // evaluates to TRUE
,因为这将被用于比较变量x和y在的决定的方法的编译时间即可。字符串是不可变的,所以没有害处使用 ==
重载支持字符串值相等。当编译器优化您的字符串,它看到两个 X
和是
有相同的值,因此你只需要一个字符串目的。它是安全的,因为字符串在C#中不可改变的。
since the method which will be used to compare variables x and y is decided at compile time. Strings are immutable so there is no harm in using ==
overloaded to support value equality for strings. When compiler optimizes your string literals, it sees that both x
and y
have same value and thus you need only one string object. It's safe because String is immutable in C#.
而当你使用等于
那么变量的类型确定。在运行基于变量x内的实际类型
Whereas when you use Equals
then the type of the variable is determined at runtime based on the actual type within the variable x.
object x = "ABCD";
object y = 'A' + "BCD"; // ensure it's a different reference
if (x == y) { // evaluates to FALSE
,而
object x = "ABCD";
object y = 'A' + "BCD"; // ensure it's a different reference
if (x.Equals(y)) { // evaluates to TRUE
你也可以检查
Also you may check Guidelines for Overriding Equals() and Operator == (C# Programming Guide)
在C#中,有两种不同类型的平等:
...]
默认情况下,运营商==由
。确定两个引用是否表示相同的对象引用相等测试。
因此,引用类型不必执行
以获得此功能的==操作符。当类型是不可变的,即,包含在该实例
中的数据不能被改变,
重载运算符==比较值相等的,而不是参考
平等可能是因为有用,为不可改变的目的,它们可以是
认为是相同的,只要它们具有相同的值。它不是覆盖非不变类型的==操作符一个
好主意。
By default, the operator == tests for reference equality by determining whether two references indicate the same object. Therefore, reference types do not have to implement operator == in order to gain this functionality. When a type is immutable, that is, the data that is contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference equality can be useful because, as immutable objects, they can be considered the same as long as they have the same value. It is not a good idea to override operator == in non-immutable types.
这篇关于试图了解与对象==操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!