HashSet limit - how to proceed?

.net, c#, gethashcode, hashset, set

Solution

`GetHashCode` does return an int, but if the comparison for the hash codes determines they are the same, it folllows by calling the `Equals` method (which you should override).

So, no, you don't have to switch. You can keep using the same old lovable `HashSet` (as long as you don't run out of memory).

Problem

My program creates custom objects, I want to get a distinct list of. So I want to use a set and add object by object. The set would prevent duplicates. And at last I have a set of unique objects. I would usually use a HashSet, because I don't need a sorted set. Only, there are so many different potential objects. More than 2^32. The GetHashCode function returns an int, so this cannot work as a unique key for my objects. I assume that I cannot use the HashSet hence and must use the slower SortedSet and have my object implement IComparable / CompareTo. Is this correct? Or is there a way to have a HashSet with long hash codes?

Original source