Export (0) Print
Expand All

Collections and Data Structures

Closely related data can be handled more efficiently when grouped together into a collection. Instead of writing separate code to handle each individual object, you can use the same code to process all the elements of a collection.

To manage a collection, you can use the System.Array class or the classes in the System.Collections, System.Collections.Generic, and System.Collections.Concurrent namespaces to add, remove, and modify either individual elements or a range of elements in the collection. Use the immutable collection classes in the System.Collections.Immutable namespace to work with collections that cannot be changed and that are modified by making a new collection. These classes are supported starting with .NET Framework 4.5. Use them to build apps that target the desktop, the Windows Store, Portable Class Library, and Windows Phone 8.

Some System.Collections classes have sorting capabilities, and most are indexed. Memory management is handled automatically, and the capacity of a collection is expanded as required. Synchronization provides thread safety when accessing members of the collection. Some System.Collections classes can generate wrappers that make the collection read-only or fixed-size, and collections in the System.Collections.Immutable namespace are immutable. Any System.Collections class can generate its own enumerator that makes it easy to iterate through the elements.

Starting with the .NET Framework 2.0, generic collection classes provide new functionality and make it easy to create strongly typed collections. See the System.Collections.Generic and System.Collections.ObjectModel namespaces.

Starting with the .NET Framework 4, the collections in the System.Collections.Concurrent namespace provide efficient thread-safe operations for accessing collection items from multiple threads. The immutable collection classes in the System.Collections.Immutable namespace are inherently thread safe because the original collection cannot be modified.

The LINQ to Objects feature enables you to use LINQ queries to access in-memory objects as long as the object type implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface. 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 and Parallel LINQ (PLINQ).



Defining Collections

Describes what collection types are, and explains the differences between generic and nongeneric collection types in the .NET Framework class library.

Commonly Used Collection Types

Describes commonly used generic and nongeneric collection types such as System.Array, System.Collections.Generic.List<T>, and System.Collections.Generic.Dictionary<TKey, TValue>.

Bit Collections

Describes System.Collections.BitArray and System.Collections.Specialized.BitVector32 collection types.

Specialized Collections

Describes special-purpose collections such as System.Collections.Specialized.NameValueCollection, System.Collections.Specialized.StringDictionary, and System.Collections.Specialized.StringCollection.

Thread-Safe Collections

Describes collection types such as System.Collections.Concurrent.BlockingCollection<T> and System.Collections.Concurrent.ConcurrentBag<T> that support safe and efficient concurrent access from multiple threads.

Creating and Manipulating Collections

Discusses how to select the best collection type, enumerate collections, use collections with multiple threads, and sort collections.

When to Use Generic Collections

Discusses the use of generic collection types.

Immutable Collections

Introduces the immutable collections and provides links to the collection types.

© 2014 Microsoft