Dela via


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

Reference