Skip to content

Latest commit

 

History

History
37 lines (28 loc) · 2.88 KB

File metadata and controls

37 lines (28 loc) · 2.88 KB
title Introduction to Generics (C# Programming Guide)
ms.date 07/20/2015
ms.prod .net
ms.technology
devlang-csharp
ms.topic article
helpviewer_keywords
generics [C#], about generics
ms.assetid a1ad761e-42f7-41dd-a62f-452a2de26b9d
caps.latest.revision 32
author BillWagner
ms.author wiwagn

Introduction to Generics (C# Programming Guide)

Generic classes and methods combine reusability, type safety and efficiency in a way that their non-generic counterparts cannot. Generics are most frequently used with collections and the methods that operate on them. Version 2.0 of the .NET Framework class library provides a new namespace, xref:System.Collections.Generic, which contains several new generic-based collection classes. It is recommended that all applications that target the [!INCLUDEdnprdnshort] 2.0 and later use the new generic collection classes instead of the older non-generic counterparts such as xref:System.Collections.ArrayList. For more information, see Generics in the .NET Framework Class Library.

Of course, you can also create custom generic types and methods to provide your own generalized solutions and design patterns that are type-safe and efficient. The following code example shows a simple generic linked-list class for demonstration purposes. (In most cases, you should use the xref:System.Collections.Generic.List%601 class provided by the .NET Framework class library instead of creating your own.) The type parameter T is used in several locations where a concrete type would ordinarily be used to indicate the type of the item stored in the list. It is used in the following ways:

  • As the type of a method parameter in the AddHead method.

  • As the return type of the Data property in the nested Node class.

  • As the type of the private member data in the nested class.

Note that T is available to the nested Node class. When GenericList<T> is instantiated with a concrete type, for example as a GenericList<int>, each occurrence of T will be replaced with int.

[!code-csharpcsProgGuideGenerics#2]

The following code example shows how client code uses the generic GenericList<T> class to create a list of integers. Simply by changing the type argument, the following code could easily be modified to create lists of strings or any other custom type:

[!code-csharpcsProgGuideGenerics#3]

See Also

xref:System.Collections.Generic
C# Programming Guide
Generics