Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

hashset

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

like image 368
BuZz Avatar asked Jun 06 '12 15:06

BuZz


People also ask

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.

Why is C not A or B?

Because C comes after B The reason why the language was named “C” by its creator was that it came after B language. Back then, Bell Labs already had a programming language called “B” at their disposal.

Why is C such an important language?

Being a middle-level language, C reduces the gap between the low-level and high-level languages. It can be used for writing operating systems as well as doing application level programming. Helps to understand the fundamentals of Computer Theories.


2 Answers

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.

like image 77
jason Avatar answered Sep 17 '22 08:09

jason


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

like image 28
GianT971 Avatar answered Sep 18 '22 08:09

GianT971