Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually, it does neither. The <code>==</code> operator by default will test for reference equality, regardless of the overridden behavior of your <code>Equals</code> method (if you've overridden it, which you certainly should have if you implemented <code>IEquatable&lt;T&gt;</code>).</p> <p>That is to say, if your variables are typed as <code>object</code> but you want to use your own custom equality comparison, use <code>Equals(x, y)</code> rather than <code>x == y</code>.</p> <p>Then, even if you've implemented <code>IEquatable&lt;T&gt;</code>, be sure to <strong>still override <code>object.Equals</code></strong>, like this:</p> <pre><code>class MyType : IEquatable&lt;MyType&gt; { public bool Equals(MyType other) { // Whatever you want. } public override bool Equals(object other) { // Presumably you check for null above. return Equals(other as MyType); } } </code></pre> <p>While you certainly <em>can</em> also overload the <code>==</code> and <code>!=</code> operators for your type, this won't accomplish anything if you have references to objects of this type that are simply <code>object</code> variables, like this:</p> <pre><code>object x = new MyType(); object y = new MyType(); Console.WriteLine(Equals(x, y)); Console.WriteLine(x == y); </code></pre> <p>The above won't work as you might expect (if you've overloaded <code>==</code> and expect that to be used) because the <code>==</code> overload has to be resolved at compile-time; since <code>x</code> and <code>y</code> are typed as arbitrary objects, the C# compiler will pick the <code>object</code> type's <code>==</code> operator, which, again, just tests for reference equality.</p> <hr> <p><strong>Update</strong>: Now, you <em>can</em> ensure your <code>==</code> operator is used if your variables are typed as the class wherein you defined it <em>or</em> a more derived type. For example, given the following types:</p> <pre><code>class A { public static bool operator ==(A x, A y) { return true; } public static bool operator !=(A x, A b) { return false; } } class B : A { } class AComparer&lt;T&gt; where T : A { public bool CompareEqual(T x, T y) { return x == y; } } </code></pre> <p>The <code>AComparer&lt;T&gt;.CompareEqual</code> method above will use your overloaded <code>==</code> operator for <em>any</em> type <code>T</code> deriving from <code>A</code>.</p> <p>The key thing to remember is that <code>==</code> is <em>static</em>, which means its overload resolution gets performed at compile-time, not at run-time using a vtable (unless you're using <code>dynamic</code>, but that's a whole other beast). So just be aware of that whenever you're using the <code>==</code> operator in code and you want the overload to resolve to that of your custom type.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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