10

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...

0

3 Answers 3

34

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.

Sign up to request clarification or add additional context in comments.

Comments

11

Overriding GetHashCode is not enough. You need to override Equals function as well.

Comments

0

Do not use hashsets to try to avoid duplicate values. Use them to balance hash tables!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.