Immutable value types should override Object.Equals to get better performance than the default implementation, which uses reflection, and should override Object.GetHashCode to avoid a bug in the default implementation. Immutable reference types should override Equals if they need to provide value equality semantics. (For example, String is a reference type, but provides value equality: two strings are equal if they have the same content, not just if they are the same object in memory.)
There are a lot of rules to follow to implement equality correctly. A concise list is presented in Framework Design Guidelines, 2nd Edition, sections 8.9.1 and 8.9.2. A detailed explanation of the rules can also be found in the documentation for Object.Equals, Implementing the Equals Method, and Guidelines for Implementing Equals and the Equality Operator(==).
IEquatable<T>.Equalsand place the primary equality comparison in this method.
Object.Equalsand implement the equality & inequality operators; these should all delegate to
Object.GetHashCodeand provide an implementation that is consistent with
== nullwill needlessly use the overloaded operator.
Posted by Bradley Grainger on February 08, 2010