Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 <strong>Equals</strong> works only on object instances. The ReferenceEquals method is static."</p> <hr> <p>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 <em>type</em>) and about which can be overridden.</p> <p>If we leave that aside, let's deal with the last bit of your question, i.e. how they work with plain<code>System.Object</code>instances and <code>System.Object</code>references (we need both to dodge the non-polymorphic nature of <code>==</code>). Here, all three operations will work <em>equivalentally</em>, but with a caveat:<code>Equals</code>cannot be invoked on<code>null</code>.</p> <p><code>Equals</code>is an instance method that takes <em>one</em> parameter (which <em>can</em> be<code>null</code>). Since it is an instance method (must be invoked on an actual object), it can't be invoked on a <code>null</code>-reference.</p> <p><code>ReferenceEquals</code> is a static method that takes <em>two</em> parameters, either / both of which can be <code>null</code>. Since it is static (not associated with an object <em>instance</em>), it will not throw a<code>NullReferenceException</code> under any circumstances. </p> <p><code>==</code>is an operator, that, in this case (<code>object</code>), behaves identically to <code>ReferenceEquals</code>. It will not throw a<code>NullReferenceException</code>either.</p> <p>To illustrate:</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload