Your implementation of GetHashCode
is wrong. Return obj.RoleID.GetHashCode();
Background:
Code that consumes an IEqualityComparer<T>
usually first compares the hash codes of two objects. Only if those hash codes are the same Equals
is called.
It is implemented like this, because two unequal objects can have the same hash key, but two equal objects never can have different hash keys - if GetHashCode()
is implemented correctly.
This knowledge is used to improve the efficiency and performance of the comparison as implementations of GetHashCode
are supposed to be fast, cheap operations.