Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>That article just recommends against overriding the equality operator (for reference types), not against overriding Equals. You should override Equals within your object (reference or value) if equality checks will mean something more than reference checks. If you want an interface, you can also implement <a href="http://msdn.microsoft.com/en-us/library/ms131187.aspx" rel="nofollow noreferrer">IEquatable</a> (used by generic collections). If you do implement IEquatable, however, you should also override equals, as the IEquatable remarks section states:</p> <blockquote> <p>If you implement IEquatable&lt;T>, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable&lt;T>.Equals method. If you do override Object.Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. This ensures that all invocations of the Equals method return consistent results. </p> </blockquote> <p>In regards to whether you should implement Equals and/or the equality operator:</p> <p>From <a href="http://msdn.microsoft.com/en-us/library/336aedhh(VS.80).aspx" rel="nofollow noreferrer">Implementing the Equals Method</a></p> <blockquote> <p>Most reference types should not overload the equality operator, even if they override Equals.</p> </blockquote> <p>From <a href="http://msdn.microsoft.com/en-us/library/7h9bszxx(VS.80).aspx" rel="nofollow noreferrer">Guidelines for Implementing Equals and the Equality Operator (==)</a></p> <blockquote> <p>Override the Equals method whenever you implement the equality operator (==), and make them do the same thing.</p> </blockquote> <p>This only says that you need to override Equals whenever you implement the equality operator. It does <em>not</em> say that you need to override the equality operator when you override Equals.</p>
 

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