Skip to content

Latest commit

 

History

History
70 lines (47 loc) · 3.59 KB

File metadata and controls

70 lines (47 loc) · 3.59 KB
title Generics (C# Programming Guide)
ms.date 07/20/2015
ms.prod .net
ms.technology
devlang-csharp
ms.topic article
helpviewer_keywords
C# language, generics
generics [C#]
ms.assetid 75ea8509-a4ea-4e7a-a2b3-cf72482e9282
caps.latest.revision 23
author BillWagner
ms.author wiwagn

Generics (C# Programming Guide)

Generics were added to version 2.0 of the C# language and the common language runtime (CLR). Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter T you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations, as shown here:

[!code-csharpcsProgGuideGenerics#1]

Generics Overview

  • Use generic types to maximize code reuse, type safety, and performance.

  • The most common use of generics is to create collection classes.

  • The .NET Framework class library contains several new generic collection classes in the xref:System.Collections.Generic namespace. These should be used whenever possible instead of classes such as xref:System.Collections.ArrayList in the xref:System.Collections namespace.

  • You can create your own generic interfaces, classes, methods, events and delegates.

  • Generic classes may be constrained to enable access to methods on particular data types.

  • Information on the types that are used in a generic data type may be obtained at run-time by using reflection.

Related Sections

For more information:

C# Language Specification

For more information, see the C# Language Specification.

See Also

xref:System.Collections.Generic
C# Programming Guide
Types
<typeparam>
<typeparamref>