Generics in the .NET Framework
Generics let you tailor a method, class, structure, or interface to the precise data type it acts upon. For example, instead of using the Hashtable class, which allows keys and values to be of any type, you can use the Dictionary<TKey, TValue> generic class and specify the type allowed for the key and the type allowed for the value. Among the benefits of generics are increased code reusability and type safety.
A number of generic collection classes are provided in the .NET Framework, in the System.Collections.Generic and System.Collections.ObjectModel namespaces. Generic interfaces for implementing sort and equality comparisons are provided in the System namespace, along with generic delegate types for event handlers, conversions, and search predicates.
Support for generics has been added to System.Reflection for examining generic types and generic methods, to System.Reflection.Emit for emitting dynamic assemblies that contain generic types and methods, and to System.CodeDom for generating source graphs that include generics.
Visual Basic, C#, and Visual C++ provide full support for defining and using generic types. The common language runtime provides new opcodes and prefixes to support generic types in Microsoft intermediate language (MSIL).
In This Section
Overview of Generics in the .NET Framework
Describes the generics feature, the capabilities it brings to the .NET Framework, and the way it is exposed in the common language runtime.Generic Collections in the .NET Framework
Describes generic collection classes and other generic types in the .NET Framework.Generic Delegates for Manipulating Arrays and Lists
Describes generic delegates for conversions, search predicates, and actions to be taken on elements of an array or collection.Generic Interfaces
Describes generic interfaces that provide common functionality across families of generic types.Advantages and Limitations of Generics
Summarizes the advantages and restrictions in the use of generics.
Reference
System.Collections.Generic
Catalogs most of the generic collection types provided by the .NET Framework, such as the List<T> and Dictionary<TKey, TValue> generic classes.System.Collections.ObjectModel
Catalogs additional generic collection types, such as the ReadOnlyCollection<T> generic class, that are useful for exposing object models to users of your classes.OpCodes
New opcodes and prefixes have been added to support generics, including Stelem, Ldelem, Unbox_Any, Constrained, and Readonly.
Related Sections
Commonly Used Collection Types
Provides summary information about the characteristics and usage scenarios of the collection types in the .NET Framework, including generic types.When to Use Generic Collections
Describes general rules for determining when to use generic collection types.Reflection and Generic Types
Explains how to use reflection to examine generic types and methods.How to: Define a Generic Type with Reflection Emit
Explains how to generate dynamic assemblies that include generic types and methods.Generic Types in Visual Basic
Describes the generics feature for Visual Basic users, including how-to topics for using and defining generic types.Introduction to Generics (C# Programming Guide)
Provides an overview of defining and using generic types for C# users.Overview of Generics in Visual C++
Describes the generics feature for C++ users, including the differences between generics and templates.