Chapter 9. Collections

Collections are groups of items; in .NET, collections contain objects (including boxed value types). Each object contained in a collection is called an element. Some collections contain a straightforward list of elements, while others (dictionaries) contain a list of key and value pairs. The following collection types consist of a straightforward list of elements:

ArrayList
BitArray
Queue
Stack

The following collection types are dictionaries:

Hashtable
SortedList

These collection classes are organized under the System.Collections namespace. In addition to this namespace, there is also another namespace called System.Collections.Specialized, which contains a few more useful collection classes. These classes might not be as well known as the previous classes, so here is a short explanation of the list:

ListDictionary

This class operates very similar to the Hashtable. However, this class beats out the Hashtable on performance when it contains 10 or fewer elements.

HybridDictionary

This class consists of two internal collections, the ListDictionary and the Hashtable. Only one of these classes is used at any one time. The ListDictionary is used while the collection contains 10 or fewer elements, and then a switch is made to use a Hashtable when the collection grows beyond 10 elements. This switch is made transparently to the developer. Once the Hashtable is used, this collection cannot revert to using the ListDictionary even if the elements number ...

Get C# Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.