Why does C # hashSet accept adding two objects with the same getHashCode () value?

I have a CustomObject that overrides GetHashCode (). I have a HashSet and I can call add with two separate objects having the same hash code. Both are added, and later I finish some problems with inserting the database (duplicates of primary keys) ... The purpose of using hashSet was related to these database inserts (avoiding key conflicts).

Perhaps I missed some HashSet properties? Even when I try to check (.Contains) before adding (.Add), I end up adding hashCode duplicates ...

+7
source share
3 answers

Since the membership of HashSet<T> based on equality of objects, and not on equality of hash code. It is perfectly legal that each member of a HashSet<T> have the same hash code if the members are different from each other according to Equals . The role that hash codes play in HashSet<T> is rapid membership testing. 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 a HashSet<T> , you need to go through the chain of objects using the same hash code test for equality using Equals to see if the object is actually in a HashSet<T> or not. Therefore, a balanced distribution of the hash code is important. But this is not the case when unique hash codes are needed.

+22
source

It is not enough to override GetHashCode. You also need to override the Equals function.

+8
source

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

+1
source

All Articles