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.
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
.
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
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.
==
is an operator, that, in this case (object
), behaves identically to ReferenceEquals
. It will not throw aNullReferenceException
either.
To illustrate:
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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…