All rules
CA2218Usage Enabled by default: As suggestion

Override GetHashCode on overriding Equals

Override GetHashCode on overriding Equals

Microsoft docs

Description

System.Object.GetHashCode returns a value, based on the current instance, that's suited for hashing algorithms and data structures such as hash tables. Two objects that are the same type and are equal must return the same hash code to ensure that instances of the following types work correctly:

  • System.Collections.Hashtable
  • System.Collections.SortedList
  • System.Collections.Generic.Dictionary`2
  • System.Collections.Generic.SortedDictionary`2
  • System.Collections.Generic.SortedList`2
  • System.Collections.Specialized.HybridDictionary
  • System.Collections.Specialized.ListDictionary
  • System.Collections.Specialized.OrderedDictionary
  • Types that implement System.Collections.Generic.IEqualityComparer`1

This rule only applies to Visual Basic code. The C# compiler generates a separate warning, CS0659.

Cause

A public type overrides System.Object.Equals but does not override System.Object.GetHashCode.

How to fix violations

To fix a violation of this rule, provide an implementation of System.Object.GetHashCode. For a pair of objects of the same type, ensure that the implementation returns the same value if your implementation of System.Object.Equals returns true for the pair.

When to suppress

Do not suppress a warning from this rule.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0