This article provides an overview of .NET's generic interfaces that provide common functionality across families of generic types.
Generic interfaces provide type-safe counterparts to nongeneric interfaces for ordering and equality comparisons, and for functionality that's shared by generic collection types. .NET 7 introduces generic interfaces for number-like types, for example, System.Numerics.INumber. These interfaces let you define generic methods that provide mathematical functionality, where the generic type parameter is constrained to be a type that implements a generic, numeric interface.
Equality and ordering comparisons
In the System namespace, the System.IComparable and System.IEquatable generic interfaces, like their nongeneric counterparts, define methods for ordering comparisons and equality comparisons, respectively. Types implement these interfaces to provide the ability to perform such comparisons.
In the System.Collections.Generic namespace, the System.Collections.Generic.IComparer and System.Collections.Generic.IEqualityComparer generic interfaces offer a way to define an ordering or equality comparison for types that don't implement the System.IComparable or System.IEquatable interface. They also provide a way to redefine those relationships for types that do.
These interfaces are used by methods and constructors of many of the generic collection classes. For example, you can pass a generic System.Collections.Generic.IComparer object to the constructor of the System.Collections.Generic.SortedDictionary class to specify a sort order for a type that does not implement generic System.IComparable. There are overloads of the System.Array.Sort generic static method and the System.Collections.Generic.List.Sort instance method for sorting arrays and lists using generic System.Collections.Generic.IComparer implementations.
The System.Collections.Generic.Comparer and System.Collections.Generic.EqualityComparer generic classes provide base classes for implementations of the System.Collections.Generic.IComparer and System.Collections.Generic.IEqualityComparer generic interfaces and also provide default ordering and equality comparisons through their respective System.Collections.Generic.Comparer.Default and System.Collections.Generic.EqualityComparer.Default properties.
Collection functionality
The System.Collections.Generic.ICollection generic interface is the basic interface for generic collection types. It provides basic functionality for adding, removing, copying, and enumerating elements. System.Collections.Generic.ICollection inherits from both generic System.Collections.Generic.IEnumerable and nongeneric System.Collections.IEnumerable.
The System.Collections.Generic.IList generic interface extends the System.Collections.Generic.ICollection generic interface with methods for indexed retrieval.
The System.Collections.Generic.IDictionary generic interface extends the System.Collections.Generic.ICollection generic interface with methods for keyed retrieval. Generic dictionary types in the .NET base class library also implement the nongeneric System.Collections.IDictionary interface.
The System.Collections.Generic.IEnumerable generic interface provides a generic enumerator structure. The System.Collections.Generic.IEnumerator generic interface implemented by generic enumerators inherits the nongeneric System.Collections.IEnumerator interface; the System.Collections.IEnumerator.MoveNext and System.Collections.IEnumerator.Reset members, which do not depend on the type parameter
T, appear only on the nongeneric interface. This means that any consumer of the nongeneric interface can also consume the generic interface.
Mathematical functionality
.NET 7 introduces generic interfaces in the System.Numerics namespace that describe number-like types and the functionality available to them. The 20 numeric types that the .NET base class library provides, for example, System.Int32 and System.Double, have been updated to implement these interfaces. The most prominent of these interfaces is System.Numerics.INumber, which roughly corresponds to a "real" number.
For more information about these interfaces, see Generic math.