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

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.

See also