In C#, is there an IEqualityComparer<IEnumerable>
that uses the SequenceEqual
method to determine equality?
Asked
Active
Viewed 6,636 times
26

Pang
- 9,564
- 146
- 81
- 122
-
2What would it use for implementing `GetHashCode`? – Sam Harwell Feb 03 '13 at 18:27
-
Not exactly what you ask, but if you restrict yourself to `T[]` (not general `IEnumerable
`), you can use `StructuralComparisons.StructuralEqualityComparer.Equals(arr1, arr2)` and `StructuralComparisons.StructuralEqualityComparer.GetHashCode(arr)`. Not the best solution from the BCL, and not so type-safe (you can wrap it in your own class deriving from `EqualityComparer – Jeppe Stig Nielsen Jun 01 '19 at 18:24` to make sure it is use on `T[]` only), but it exists.
2 Answers
26
There is no such comparer in .NET Framework, but you can create one:
public class IEnumerableComparer<T> : IEqualityComparer<IEnumerable<T>>
{
public bool Equals(IEnumerable<T> x, IEnumerable<T> y)
{
return Object.ReferenceEquals(x, y) || (x != null && y != null && x.SequenceEqual(y));
}
public int GetHashCode(IEnumerable<T> obj)
{
// Will not throw an OverflowException
unchecked
{
return obj.Where(e => e != null).Select(e => e.GetHashCode()).Aggregate(17, (a, b) => 23 * a + b);
}
}
}
In the above code, I iterate over all items of the collection in the GetHashCode
. I don't know if it's the wisest solution but this what is done in the internal HashSetEqualityComparer
.

Ben Gripka
- 16,012
- 6
- 45
- 41

Cédric Bignon
- 12,892
- 3
- 39
- 51
-
FYI the current hashing in `HashSetEqualityComparer` is not what you have here. It's not what you want anyway. It doesn't compare a *sequence* but a *set*- its hash purposely collides between sequences that have the same items in different orders. Your code here is fine. – jnm2 Jan 06 '17 at 18:08
-
Good implementation, can the unchecked be better off only enclosing the actual calculation which can overflow? – too Apr 05 '19 at 10:16
-
Note that depending on what you plan to do with the thing, it may be reasonable to simply implement the hash code as `int GetHashCode(IEnumnerable
object) => 0;` – dlf Dec 13 '19 at 13:38
1
Created a NuGet package based on Cédric Bignon's answer:
Assembly package: https://www.nuget.org/packages/OBeautifulCode.Collection/
Code-file-only package: https://www.nuget.org/packages/OBeautifulCode.Collection.Recipes.EnumerableEqualityComparer/
var myEqualityComparer = new EnumerableEqualityComparer<string>();