The Comparers library has four main parts:
- Comparer implementations. You can use the
ComparerBuilder
andEqualityComparerBuilder
classes to create comparer instances with a fluent API. - Extension methods for comparers. The
ComparerExtensions
andEqualityComparerExtensions
classes provide extensions that can be used to modify any comparer (including custom or built-in comparers). - Assistance for a type defining its own default comparer. The
ComparableBase
andEquatableBase
classes help derived classes define a reasonable default comparer. - Extension methods for LINQ to Objects. All operators that take a comparer get overloads that allow a fluent API syntax for defining a comparer right within the LINQ query. This is particularly useful for defining comparers over anonymous types.
Every full comparer provided by this library implements IFullComparer<T>
, which derives from all four interfaces IComparer<T>
, IComparer
, IEqualityComparer<T>
, and IEqualityComparer
. Every equality comparer provided by this library implements IFullEqualityComparer<T>
, which derives from both IEqualityComparer<T>
and IEqualityComparer
.
This means that the comparers generated by this library can be used with any generic or non-generic container or algorithm. Also, all the comparers implement equality comparison as well, so they can be used with hash-based containers and algorithms.
Since the .NET Comparer<T>.Default
type does not implement IEqualityComparer<T>
, this library provides its own default comparer.
For more advanced uses, this library also supports building comparers at runtime and defining one comparer that can be used with multiple unrelated types.