GetHashCode
The method that returns a number summarising an object, used by hash-based collections such as Dictionary and HashSet to find it quickly.
In more detail
Equal objects must return equal hash codes, so if you override Equals you must override GetHashCode from the same members. Otherwise equal objects land in different buckets and never find each other.
Related terms
- value equality — Treating two objects as equal when their members hold equal values, even if they are separate objects.
- Dictionary<TKey, TValue> — The .NET collection that maps unique keys to values and finds a value by its key in constant time by hashing the key.
- HashSet<T> — The .NET collection of unique items that answers 'have I seen this one already?' in constant time.
Taught in
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices