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

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.
  • Commonly Used Collection Types
    Provides summary information about the characteristics and usage scenarios of the collection types in the .NET Framework, including generic types.
  • Generic Types in Visual Basic
    Describes the generics feature for Visual Basic users, including how-to topics for using and defining generic types.