Permalink
Fetching contributors…
Cannot retrieve contributors at this time
58 lines (45 sloc) 4.43 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Generic Collections in the .NET Framework
03/30/2017
.net
dotnet-standard
article
generics [.NET Framework], collections
generic collections [.NET Framework]
5b646751-6ab7-465c-916c-b1a76aefa9f5
8
mairaw
mairaw
wpickett

Generic Collections in the .NET Framework

This topic provides an overview of the generic collection classes and other generic types in the .NET Framework.

Generic Collections in the .NET Framework

The .NET Framework class library provides a number of generic collection classes in the xref:System.Collections.Generic and xref:System.Collections.ObjectModel namespaces. For more information about these classes, see Commonly Used Collection Types.

System.Collections.Generic

Many of the generic collection types are direct analogs of nongeneric types. xref:System.Collections.Generic.Dictionary%602 is a generic version of xref:System.Collections.Hashtable; it uses the generic structure xref:System.Collections.Generic.KeyValuePair%602 for enumeration instead of xref:System.Collections.DictionaryEntry.

xref:System.Collections.Generic.List%601 is a generic version of xref:System.Collections.ArrayList. There are generic xref:System.Collections.Generic.Queue%601 and xref:System.Collections.Generic.Stack%601 classes that correspond to the nongeneric versions.

There are generic and nongeneric versions of xref:System.Collections.Generic.SortedList%602. Both versions are hybrids of a dictionary and a list. The xref:System.Collections.Generic.SortedDictionary%602 generic class is a pure dictionary and has no nongeneric counterpart.

The xref:System.Collections.Generic.LinkedList%601 generic class is a true linked list. It has no nongeneric counterpart.

System.Collections.ObjectModel

The xref:System.Collections.ObjectModel.Collection%601 generic class provides a base class for deriving your own generic collection types. The xref:System.Collections.ObjectModel.ReadOnlyCollection%601 class provides an easy way to produce a read-only collection from any type that implements the xref:System.Collections.Generic.IList%601 generic interface. The xref:System.Collections.ObjectModel.KeyedCollection%602 generic class provides a way to store objects that contain their own keys.

Other Generic Types

The xref:System.Nullable%601 generic structure allows you to use value types as if they could be assigned null. This can be useful when working with database queries, where fields that contain value types can be missing. The generic type parameter can be any value type.

[!NOTE] In C# it is not necessary to use xref:System.Nullable%601 explicitly because the language has syntax for nullable types.

The xref:System.ArraySegment%601 generic structure provides a way to delimit a range of elements within a one-dimensional, zero-based array of any type. The generic type parameter is the type of the array's elements.

The xref:System.EventHandler%601 generic delegate eliminates the need to declare a delegate type to handle events, if your event follows the event-handling pattern used by the [!INCLUDEdnprdnshort]. For example, suppose you have created a MyEventArgs class, derived from xref:System.EventArgs, to hold the data for your event. You can then declare the event as follows:

[!code-cppConceptual.Generics.Overview#7] [!code-csharpConceptual.Generics.Overview#7] [!code-vbConceptual.Generics.Overview#7]

See Also

xref:System.Collections.Generic?displayProperty=nameWithType
xref:System.Collections.ObjectModel?displayProperty=nameWithType
Generics
Generic Delegates for Manipulating Arrays and Lists
Generic Interfaces