Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 When to Use Generic Collections
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Developer's Guide
When to Use Generic Collections

Updated: November 2007

Using generic collections is generally recommended, because you gain the immediate benefit of type safety without having to derive from a base collection type and implement type-specific members. Generic collection types also generally perform better than the corresponding nongeneric collection types (and better than types that are derived from nongeneric base collection types) when the collection elements are value types, because with generics there is no need to box the elements.

The following generic types correspond to existing collection types:

There are several generic collection types that do not have nongeneric counterparts:

The LINQ to Objects feature allows you to use LINQ queries to access in-memory objects as long as the object type implements IEnumerable or IEnumerable<(Of <(T>)>). LINQ queries provide a common pattern for accessing data; are typically more concise and readable than standard foreach loops; and provide filtering, ordering and grouping capabilities. LINQ queries can also improve performance. For more information, see LINQ to Objects.

Some of the generic types have functionality that is not found in the nongeneric collection types. For example, the List<(Of <(T>)>) class, which corresponds to the nongeneric ArrayList class, has a number of methods that accept generic delegates, such as the Predicate<(Of <(T>)>) delegate that allows you to specify methods for searching the list, the Action<(Of <(T>)>) delegate that represents methods that act on each element of the list, and the Converter<(Of <(TInput, TOutput>)>) delegate that lets you define conversions between types.

The List<(Of <(T>)>) class allows you to specify your own IComparer<(Of <(T>)>) generic interface implementations for sorting and searching the list. The SortedDictionary<(Of <(TKey, TValue>)>) and SortedList<(Of <(TKey, TValue>)>) classes also have this capability, and in addition allow the comparers to be specified when the collection is created. In similar fashion, the Dictionary<(Of <(TKey, TValue>)>) and KeyedCollection<(Of <(TKey, TItem>)>) classes allow you to specify your own equality comparers.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker