试图了解与对象==操作符 [英] Trying to understand == operator with objects

查看:127
本文介绍了试图了解与对象==操作符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)?

推荐答案

虽然的Ed S的回答 == 检查引用equality.Just添加点 MSDN 链接也说了同样的事情。

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。对于字符串类型,==比较
字符串的值。

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.

如果你正在寻找那么比较对象,你因为你问的==和的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

你也可以检查 指引重写equals()和运营商==(C#编程指南)

Also you may check Guidelines for Overriding Equals() and Operator == (C# Programming Guide)

在C#中,有两种不同类型的平等:<强>引用相等
(也称为身份)和值相等价值平等是
一般理解平等的含义:它意味着两个对象
包含相同的值。 例如,两个整数用2
值具有价值的平等。参考平等意味着没有两个
要比较的对象。相反,有两个对象的引用,其中两个
指向同一对象

In C#, there are two different kinds of equality: reference equality (also known as identity) and value equality. Value equality is the generally understood meaning of equality: it means that two objects contain the same values. For example, two integers with the value of 2 have value equality. Reference equality means that there are not two objects to compare. Instead, there are two object references and both of them refer to the same object.

...]

默认情况下,运营商==由
。确定两个引用是否表示相同的对象引用相等测试。
因此,引用类型不必执行
以获得此功能的==操作符。当类型是不可变的,即,包含在该实例
中的数据不能被改变,
重载运算符==比较值相等的,而不是参考
平等可能是因为有用,为不可改变的目的,它们可以是
认为是相同的,只要它们具有相同的值。它不是覆盖非不变类型的==操作符一个
好主意。

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.

这篇关于试图了解与对象==操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆