Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does SequenceEqual take an IEqualityComparer rather than a predicate?

Why does Enumerable.SequenceEqual take its comparer as an IEqualityComparer? The algorithm seems to make no use of GetHashCode. Why does it not instead take a Func<TSource, TSource, bool> predicate, similar to how First takes a Func<TSource, bool>?

like image 825
Edward Brey Avatar asked Nov 02 '22 01:11

Edward Brey


1 Answers

I am tempted to say "because".

If you look around at other similar methods (like Enumerable.Distinct) they also take an IEqualityComparer in the overload.

Also, an IEqualityComparer is the "correct" way to check if objects are equal. A Func<TSource, TSource, bool> would not neccessarily check for equality, it would check if the objects are similar enough for your specific usage at this moment in time.

Luckily it is easy enough to make your own extension method. For instance does MoreLinq have a implementation of DistinctBy that you can look at.

like image 168
aanund Avatar answered Dec 29 '22 04:12

aanund