Generic interfaces in .NET
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<TSelf>. 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.
Note
The type parameters of several generic interfaces are marked covariant or contravariant, providing greater flexibility in assigning and using types that implement these interfaces. For more information, see Covariance and Contravariance.
Equality and ordering comparisons
In the System namespace, the System.IComparable<T> and System.IEquatable<T> 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 IComparer<T> and IEqualityComparer<T> generic interfaces offer a way to define an ordering or equality comparison for types that don't implement the System.IComparable<T> or System.IEquatable<T> 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 IComparer<T> object to the constructor of the SortedDictionary<TKey,TValue> class to specify a sort order for a type that does not implement generic System.IComparable<T>. There are overloads of the Array.Sort generic static method and the List<T>.Sort instance method for sorting arrays and lists using generic IComparer<T> implementations.
The Comparer<T> and EqualityComparer<T> generic classes provide base classes for implementations of the IComparer<T> and IEqualityComparer<T> generic interfaces and also provide default ordering and equality comparisons through their respective Comparer<T>.Default and EqualityComparer<T>.Default properties.
Collection functionality
The ICollection<T> generic interface is the basic interface for generic collection types. It provides basic functionality for adding, removing, copying, and enumerating elements. ICollection<T> inherits from both generic IEnumerable<T> and nongeneric IEnumerable.
The IList<T> generic interface extends the ICollection<T> generic interface with methods for indexed retrieval.
The IDictionary<TKey,TValue> generic interface extends the ICollection<T> generic interface with methods for keyed retrieval. Generic dictionary types in the .NET base class library also implement the nongeneric IDictionary interface.
The IEnumerable<T> generic interface provides a generic enumerator structure. The IEnumerator<T> generic interface implemented by generic enumerators inherits the nongeneric IEnumerator interface; the MoveNext and 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, Int32 and Double, have been updated to implement these interfaces. The most prominent of these interfaces is INumber<TSelf>, which roughly corresponds to a "real" number.
For more information about these interfaces, see Generic math.