krisgithub1234 / linqbridge

Automatically exported from code.google.com/p/linqbridge
Other
0 stars 0 forks source link

Enumerable.SequenceEqual disregarding IEquatable<T> #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The Enumerable.SequenceEqual<TSource>(...) extension method does not 
perform a correct comparison of the sequence's elements. The MSDN 
documentation for this method states that:

The SequenceEqual<TSource>(IEnumerable<TSource>), IEnumerable<TSource>)) 
method enumerates the two source sequences in parallel and compares 
corresponding elements by using the default equality comparer for TSource, 
Default.

The default comparer, EqualityComparer<T>.Default states that:

The Default property checks whether type T implements the 
System.IEquatable<T> generic interface and if so returns an 
EqualityComparer<T> that uses that implementation. Otherwise it returns an 
EqualityComparer<T> that uses the overrides of Object.Equals and 
Object.GetHashCode provided by T.

So you would expect to be able to pass a sequence of objects that 
implement the IEquatable<T> interface and have the 
Enumerable.SequenceEqual method to work correctly, but in practice this 
won't work. The line that performs the comparison (Query 
Operators\Quantifiers.cs, line 59) calls Object.Equals(Object) method:

if (!object.Equals (firstRator.Current, secondElement)) return false;

Which results in it always returning false for sequences of object that 
only implement IEquatable<T>, instead of using EqualityComparer<T>.Default 
as so:

if (!EqualityComparer<TSource>.Default.Equals(firstRator.Current, 
secondElement)) return false;

Attached is the corrected (and tested) Quantifiers.cs.

Original issue reported on code.google.com by allon.guralnek on 24 Sep 2009 at 10:49

Attachments:

GoogleCodeExporter commented 9 years ago
Allon, I'd like to add you as a contributor to the project. What are your 
details?

Joe

Original comment by joe.albahari@gmail.com on 25 Sep 2009 at 12:07

GoogleCodeExporter commented 9 years ago
What do you mean by details?

Original comment by allon.guralnek on 25 Sep 2009 at 12:11

GoogleCodeExporter commented 9 years ago
Saw you gave me commit permissions (thanks!) so I committed the fix.

Original comment by allon.guralnek on 25 Sep 2009 at 4:28