A collection is a set of similarly typed objects that are grouped together.
Objects of any type can be grouped into a single collection of the type Object to take advantage of constructs that are inherent in the language. For example, the C# foreach statement (for each in Visual Basic) expects all objects in the collection to be of a single type.
However, in a collection of type Object, additional processing is done on the elements individually, such as boxing and unboxing or conversions, which affect the performance of the collection. Boxing and unboxing typically occur if storing or retrieving a value type in a collection of type Object.
Strongly typed collections, such as StringCollection, avoid these performance hits, if the type of the element is the type that the collection is intended for (for example, storing or retrieving strings from a StringCollection). In addition, strongly typed collections automatically perform type validation of each element added to the collection.
All collections that directly or indirectly implement the ICollection interface share several features in addition to methods that add, remove, or search elements:
- An enumerator. An enumerator is an object that iterates through its associated collection. It can be thought of as a movable pointer to any element in the collection. An enumerator can be associated with only one collection, but a collection can have multiple enumerators. The C# foreach statement (for each in Visual Basic) uses the enumerator and hides the complexity of manipulating the enumerator.
- Synchronization members. Synchronization provides thread safety when accessing elements of the collection. The collections are not thread safe by default. Only a few classes in the Collections namespace provide a Synchronize method that creates a thread-safe wrapper over the collection. However, all classes in the Collections namespace provide a SyncRoot property that can be used by derived classes to create their own thread-safe wrapper. An IsSynchronized property is also provided to determine whether the collection is thread safe.
- The CopyTo method. All collections can be copied to an array using the CopyTo method; however, the order of the elements in the new array is based on the sequence in which the enumerator returns them. The resulting array is always one-dimensional with a lower bound of zero.
The following features are implemented in some classes in the Collections namespace:
- Capacity and count. The capacity of a collection is the number of elements it can contain. The count of a collection is the number of elements it actually contains. A BitArray is a special case; its capacity is the same as its length, which is the same as its count. Some collections hide the capacity or the count or both.
All collections in the Collections namespace automatically expand in capacity when the current capacity is reached. The memory is reallocated, and the elements are copied from the old collection to the new one. This reduces the code required to use the collection; however, the performance of the collection might still be negatively affected. The best way to avoid poor performance caused by multiple reallocations is to set the initial capacity to be the estimated size of the collection.
- Lower Bound. The lower bound of a collection is the index of its first element. All indexed collections in the Collections namespace have a lower bound of zero. Array has a lower bound of zero by default, but a different lower bound can be defined when creating an instance of the Array class using Array.CreateInstance.
Collections classes can generally be categorized into three types:
- Generic collections. These are the common variations of data collections, such as hash tables, queues, stacks, dictionaries, and lists.
- Bit collections. These are collections whose elements are bit flags. They behave slightly differently from other collections.
- Specialized collections. These are collections with highly specific purposes, usually to handle a specific type of element, like the StringDictionary.
Be sure to choose a collection class carefully. Because each collection has its own functionality, each also has its own limitations. The more specialized a collection is, the more limited it is. For tips on choosing a collection, see Selecting a Collection Class.