Export (0) Print
Expand All

Generic Interfaces

This topic provides an overview of 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 is shared by generic collection types.

Note Note

Starting with the .NET Framework 4, the type parameters of several generic interfaces are marked covariant or contravariant, providing greater flexibility in assigning and using types that implement these interfaces. See Covariance and Contravariance in Generics.

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 do not implement the System.IComparable<T> or System.IEquatable<T> generic interface, and they 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.

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 Framework 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.

Show:
© 2014 Microsoft