问题描述
我的这三个理解是:
-
.Equals()
数据相等(对于缺乏一个更好的描述)的测试。.Equals()
可返回True为同一对象的不同实例,这是最常用的方法重写
.Equals()
tests for data equality (for the lack of a better description)..Equals()
can return True for different instances of the same object, and this is the most commonly overridden method.
.ReferenceEquals()
测试两个对象是否是相同的实例,并且不能被重写。
.ReferenceEquals()
tests whether or not two objects are the same instance and cannot be overridden.
==
默认情况下是一样的的ReferenceEquals()
,但这是可以覆盖
==
is the same as the ReferenceEquals()
by default, but this CAN be overridden.
但的状态:
在Object类中,等于
和
的ReferenceEquals
方法
语义上等同,但
在的ReferenceEquals
仅适用
对象实例。该
的ReferenceEquals
方法是静态的。
现在我不明白这一点。任何人都可以阐明这一些轻?
Now I don't get it. Can anyone shed some light on this?
推荐答案
您混乱的根源似乎有从C#站提取物,应该读错字:......除了等于仅适用于对象实例。该的ReferenceEquals方法是静态的。
The source of your confusion appears to be that there is a typo in the extract from C# station, which should read: "... except that the Equals works only on object instances. The ReferenceEquals method is static."
您即将在每个语义含义的差异松散正确的(尽管同一对象的不同实例似乎有点困惑,这也许应该读的不同实例相同的键入的)大约可以覆盖。
You are loosely correct about the differences in the semantic meanings of each (although "different instances of the same object" seems a little confused, it should probably read "different instances of the same type) and about which can be overridden.
如果我们离开了一边,让我们来处理你的问题,也就是说,它们与普通的 System.Object的
实例和系统是如何工作的最后一位.Object
引用(我们既需要闪避的非多态性性质==
)。在这里,所有三个操作将工作的 equivalentally 的,但有一个警告:等于
不能在调用空
。
If we leave that aside, let's deal with the last bit of your question, i.e. how they work with plainSystem.Object
instances and System.Object
references (we need both to dodge the non-polymorphic nature of ==
). Here, all three operations will work equivalentally, but with a caveat:Equals
cannot be invoked onnull
.
等于
是需要一个实例方法的有一个的参数(可以的是空
)。因为它是一个实例方法(必须是实际的对象上调用),它不能在空
-reference调用。
Equals
is an instance method that takes one parameter (which can benull
). Since it is an instance method (must be invoked on an actual object), it can't be invoked on a null
-reference.
的ReferenceEquals
是需要一个静态方法两个的参数,无论/两者的可以空
。由于它是静态的(不与对象的相关实例的),它不会抛出的NullReferenceException
在任何情况下。
ReferenceEquals
is a static method that takes two parameters, either / both of which can be null
. Since it is static (not associated with an object instance), it will not throw aNullReferenceException
under any circumstances.
==
是一个操作符,即,在此情况下(对象
)的行为与的ReferenceEquals
。它不会抛出一个的NullReferenceException
或者
==
is an operator, that, in this case (object
), behaves identically to ReferenceEquals
. It will not throw aNullReferenceException
either.
要说明:
object o1 = null;
object o2 = new object();
//Technically, these should read object.ReferenceEquals for clarity, but this is redundant.
ReferenceEquals(o1, o1); //true
ReferenceEquals(o1, o2); //false
ReferenceEquals(o2, o1); //false
ReferenceEquals(o2, o2); //true
o1.Equals(o1) //NullReferenceException
o1.Equals(o2) //NullReferenceException
o2.Equals(o1) //false
o2.Equals(o2) //true
这篇关于C#.Equals(),.ReferenceEquals()和==操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!