Creating and Manipulating Collections

The most common collections are provided by the .NET Framework. You can use any of these or create your own collection based on one of these. Each collection is designed for specific purposes. Whenever possible, you should prefer the generic collections in the System.Collections.Generic or the System.Collections.Concurrent namespaces to the legacy types in the System.Collections namespace.

If you decide to implement your own collection, use the following guidelines:

  • Start with the right base class and interfaces. See Selecting a Collection Class for tips on how to choose a collection type.

  • Consider making your collection strongly typed. Strongly typed collections provide automatic type validation at compile time as well as run time and avoid processes that adversely affect performance, such as boxing, unboxing, and conversions. If your language supports generics, use one of the System.Collections.Generic types. If your language does not support generics, System.Collections.Specialized contains examples of strongly typed collections.

  • If your collection will be accessed from multiple threads, use the classes in the System.Collections.Concurrent namespace.

  • Consider enabling serialization for your class. See Serialization Concepts for details.



Selecting a Collection Class

Provides guidance for selecting the most appropriate collection type.

Enumerating a Collection

Discusses enumerators and their use with collections.

Thread-Safe Collections

Discusses the use of collections in multithreaded applications.

Comparisons and Sorts Within Collections

Discusses the use of equality comparisons and sorting comparisons in collections.

Collections and Data Structures

Discusses the various collection types available in the .NET Framework, including stacks, queues, lists, arrays, and structs.

Serialization Concepts

Discusses two scenarios where serialization is useful: when persisting data to storage and when passing objects across application domains.