I tried to find out how C# goes about comparing objects in a HashSet for equality.
I couldn’t find anything here: http://msdn.microsoft.com/en-us/library/bb359438.aspx
Only when I came to stackoverflow, I read that it uses the Equals() and maybe getHashCode()
I was planning to implement both methods anyways, but my question is:
What would you do to find out how HashSet actually compares objects?
It compares objects for equality using
Equals. It determines which bucket to place them in usingGetHashCode.More generically, HashSet uses the
IEqualityComparer<T>passed in to its constructor to do both. If none is specified, it usesEqualityComparer<T>.Defaultwhich calls the object’sGetHashCode()andIEquatable<T>.Equals()method (orobject.Equals()if the type doesn’t implementIEquatable<T>).