A class that overrides only one of Equals(object) and GetHashCode() is likely to violate the contract of the GetHashCode() method. The contract requires that GetHashCode() gives the same integer result for any two equal objects. Not enforcing this property may cause unexpected results when storing and retrieving objects of such a class in a hashing data structure.

Provide an implementation of the missing method that is consistent with the present method.

In the following example, the class Bad overrides Equals(object) but not GetHashCode().

In the revised example, the class Good overrides both Equals(object) and GetHashCode().

  • MSDN: Object.Equals Method (Object), Object.GetHashCode Method.