Description
If you want to return distinct elements from sequences of objects of some custom data type, you have to implement the
IEquatable<T>
generic interface in the class. The following code example shows how to implement this interface in a custom data type and provideGetHashCode
andEquals
methods.
This is confusing and wrong. IEquatable<T>
interface only (!) requires to implement the bool Equals(T other);
method. However, inside Distinct()
(and probably other similar methods such as group by, etc) also compares the hash codes of the object, which it takes from the EqualityComparer<T>.Default
, which, while being created for an implementation of IEquatable<T>
, that does not have the override for GetHashCode(T object)
, uses the default T.GetHashCode()
.
Consequence: if I follow the "must" part of the doc and implement ONLY IEquatable<T>
interface, that method (bool Equals(T other);
) even isn't being called! Which naturally totally confuses me as for what did I do wrong.
How to fix: TBH I'd say the implementation here with IEquatable<T>
itself is very questionable. It should've been IEqualityComparer. But for the docs, please at least add the note that "If you want to return distinct elements from sequences of objects of some custom data type, you have to implement the IEquatable<T>
generic interface in the class AND override the inherited GetHashCode()
method. For consistency, after that you should also override the inherited Equals()
method. ..."