Why is C# hashSet accepting the adding of two objects with same getHashCode() value?

c#, hashset

Solution

Because `HashSet<T>` membership is based on object equality, not hash code equality. It's perfectly legal for every member of a `HashSet<T>` to have the same hash code as long as the members are different according to `Equals`. The role that hash codes play in `HashSet<T>` is for rapid testing of membership. If you have an object and its hash code is not in the `HashSet<T>`, then you know that the object is not in the `HashSet<T>`. If you have an object and its hash code is in the `HashSet<T>`, then you have to walk the chain of objects with that same hash code testing for equality using `Equals` to see if the object is actually in the `HashSet<T>` or not. That's why a balanced hash code distribution is important. But it is not the case that unique hash codes are necessary.

Problem

I have a CustomObject object which overrides GetHashCode(). I have a HashSet, and I am able to call add with two distinct object having the same hash code. Both get added and later on I end up with some database insertion troubles (primary key duplicates)... The purpose of using a hashSet was connected to these database insertions (avoiding key collisions). Am I possibly missing out on some properties of HashSet ? Even when I try checking (.Contains) before adding (.Add), I end up adding hashCode duplicates...

Original source