I want to get distinct items from List in C# by using IEqualityComparer interface. But I don’t know about GetHashCode. I have implement both GetHashCode and Equals methods. And how can I call Equals method to get distinct items from a list having user define data type.
I want to get distinct items from List in C# by using IEqualityComparer interface.
Share
Use the overload of
Enumerable.Distinctthat takes anIEqualityComparerto get the distinct items from a sequence using your custom equality comparer.So that the
IEqualityComparercan be used as a test for equality in a hash table (hash the items as per theIEqualityComparer.GetHashCodemethod, useIEqualityComparer.Equalsto check for equality when needed (searching for an item in the hash table, for example).