HashSet<T>.Add
first compares the results of GetHashCode
. If those are equal, it calls Equals
.
Now, my understanding is in order to implement GetHashCode
, something must be done with the fields of an object. A simple example implementation can be found at What is the best algorithm for an overridden System.Object.GetHashCode?.
In my test comparing both on 1.000.000 pairs of objects filled with random data, performance is more or less equal between the two. GetHashCode
is implemented as in the linked example, Equals
simply calls Equals
on all fields. So why would one want to use GetHashCode
over Equals
?
GetHashCode
on SO. – Fernferna