Hash Function Added To The PredicateEqualityComparer

Posted by Paulo Morgado on ASP.net Weblogs See other posts from ASP.net Weblogs or by Paulo Morgado
Published on Wed, 13 Jun 2012 11:30:18 GMT Indexed on 2012/06/15 15:17 UTC
Read the original article Hit count: 187

Filed under:
|
|
|
|

Sometime ago I wrote a predicate equality comparer to be used with LINQ’s Distinct operator.

The Distinct operator uses an instance of an internal Set class to maintain the collection of distinct elements in the source collection which in turn checks the hash code of each element (by calling the GetHashCode method of the equality comparer) and only if there’s already an element with the same hash code in the collection calls the Equals method of the comparer to disambiguate.

At the time I provided only the possibility to specify the comparison predicate, but, in some cases, comparing a hash code instead of calling the provided comparer predicate can be a significant performance improvement, I’ve added the possibility to had a hash function to the predicate equality comparer.

You can get the updated code from the PauloMorgado.Linq project on CodePlex,

© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about .net4.0