C hashcode for array of ints
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, an int[] is a reference type, so its default hash code is based on object identity, not on the numbers inside the array. That matters when you want two arrays with the same contents to behave as equal keys in a dictionary or set.
Default Array Hash Codes Are Not Content-Based
Consider two different arrays that hold the same values:
Both lines are typically False. The arrays contain the same integers, but they are different objects. By default, int[] equality is reference equality, and the hash code follows that same identity-based rule.
If you need content-based behavior, you must supply it yourself.
Use a Custom Comparer for Dictionaries and Sets
The usual fix is to implement IEqualityComparer<int[]> and compute the hash from the elements.
Now you can use the comparer with a dictionary:
That lookup succeeds because equality and hashing are both based on array contents.
Why HashCode Is a Good Modern Choice
Older examples often multiply by primes manually:
This works, and it is still a reasonable pattern. In modern C#, the HashCode type is usually cleaner and communicates intent better. It also avoids hand-rolling a mixing strategy unless you have a specific reason to do so.
Equality and Hash Code Must Match
The most important rule is consistency:
- if two arrays are equal by your comparer, they must produce the same hash code
- if two arrays have the same hash code, they are not required to be equal
That second point surprises people. Hash collisions are allowed. A hash code only narrows the search; equality decides the final answer.
This is why implementing only GetHashCode is not enough. If you want content-based behavior, Equals must compare the elements too.
Beware of Mutable Keys
Arrays are mutable. That makes them risky as dictionary keys even with a correct comparer.
After mutation, the dictionary can behave unexpectedly because the stored key's logical hash has changed. For that reason, immutable key types are often safer than raw arrays. If the data is supposed to represent a stable value, consider using an immutable collection or a small wrapper type that never exposes mutation.
When You Do Not Need a Custom Hash Code
If you are only printing, comparing manually, or doing one-off checks, you may not need a custom comparer at all. For example:
That solves equality for a single comparison. The custom hash code becomes necessary when the array participates in hash-based collections such as Dictionary or HashSet.
Common Pitfalls
- Expecting
int[]to compare by contents automatically. It does not. - Overriding only hashing logic but not equality logic.
- Using mutable arrays as keys and then changing them after insertion.
- Forgetting to handle
nullin a custom comparer. - Assuming equal hash codes prove equality. They only mean the values share a bucket candidate.
Summary
- The default hash code for
int[]is based on reference identity, not the array contents. - For content-based dictionary or set behavior, provide an
IEqualityComparer<int[]>. - A good comparer must implement both element-wise
Equalsand consistentGetHashCode. - '
HashCodeis a clean modern way to combine element hashes.' - Mutable arrays are fragile keys, so prefer immutable value types when possible.

