Search code examples
.net.net-4.0iequalitycomparer

When to use IEqualityComparer in an app targeting .NET 4.0


Is there any benefit for me to implement the weakly typed IEqualityComparer in .NET 4.0 apps in addition to the IEqualityComparer<T> interface?

Another angle is I can always implement IEqualityComparer<System.Object> to make up an equally weakly typed scenario and never need to resort to IEqualityComparer for new code.


Solution

  • If you just derive from EqualityComparer<T> then you don't have to worry about it because it implements both IEqualityComparer and IEqualityComparer<T>. So you get weak typing for free when you implement strong typing.

    That said, it's reasonably unlikely that you'll find yourself needing the weakly-typed version. Only a handful of BCL classes use it, and they're not common ones.