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…
Because
HashSet<T>membership is based on object equality, not hash code equality. It’s perfectly legal for every member of aHashSet<T>to have the same hash code as long as the members are different according toEquals. The role that hash codes play inHashSet<T>is for rapid testing of membership. If you have an object and its hash code is not in theHashSet<T>, then you know that the object is not in theHashSet<T>. If you have an object and its hash code is in theHashSet<T>, then you have to walk the chain of objects with that same hash code testing for equality usingEqualsto see if the object is actually in theHashSet<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.