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 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.
- Advantages of Generics
Summarizes the advantages of using generics.
Reference
- System.Collections.Generic
Catalogs most of the generic collection types provided by the .NET Framework, such as the List and Dictionary generic classes.
- System.Collections.ObjectModel
Catalogs additional generic collection types, such as the ReadOnlyCollection 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.