This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

The Specialised Collections

The forty-third part of the C# Fundamentals tutorial describes the specialised (or specialized) collection classes. The .NET framework provides five different types of specialised collection, each being optimised for a particular structure or data type.

Specialised Collections

The .NET framework includes a several specialised collections designed for specific purposes or data types. These are used less often than the general-purpose collections and dictionaries described in previous articles in the C# Fundamentals tutorial. However, developers should understand their use as they can provide more appropriate and better performing collections in some circumstances.

Due to their more specialised use, Microsoft has created the System.Collections.Specialized namespace for these classes. To use the class names directly when programming, add the using System.Collections.Specialized; directive to your source code.

The ListDictionary Collection

The ListDictionary class provides a linked list structure for storing key / value pairs. A linked list is a standard programming structure used to store a series of elements, or nodes. Each node has a pointer to the next item in the list and, optionally, a second pointer to the previous item. The ListDictionary is singly-linked as it has one link to the next item. Linked lists are useful because they can provide ordered data without physically sorting the information. As the contents of the collection change, only the pointers around the changed data need to be modified.

The ListDictionary is faster and uses less memory than a Hashtable when the number of entries in the collection is small. If the number of nodes is likely to be greater than ten then a Hashtable or HybridDictionary may be more suitable.

Implemented Collection Interfaces

The ListDictionary collection implements the properties and methods of the ICollection and IDictionary interfaces.

The HybridDictionary Collection

The HybridDictionary class provides a dictionary that adapts its behaviour according to the number of key / value pairs in the collection. When the number of elements is low, each item is stored in a ListDictionary giving a performance advantage. As the size of the collection grows past the optimal number of nodes for a linked list, the storage is switched to use a Hashtable. This makes the HybridDictionary a good choice when the size of the collection is unknown.

Implemented Collection Interfaces

The HybridDictionary collection implements ICollection and IDictionary.

The StringCollection Collection

The StringCollection class is an IList-based collection that stores strings. As with other collections, the items may be duplicated and can be null.

Implemented Collection Interfaces

The StringCollection collection implements ICollection and IList.

The StringDictionary Collection

The StringDictionary collection provides similar functionality to the Hashtable class. The main difference between the two dictionary types is that whilst the Hashtable accepts an object for the key and value for each item, the StringDictionary requires that both are strings.

The key for each item in the collection must be unique and may not be null. The key is translated to lower case when it is added to the dictionary so it is not possible to add items where the keys differ only in the use of capitalisation. The value part of the key / value pair can be null and duplicates are permitted.

The NameValueCollection Collection

The NameValueCollection class is similar to StringDictionary as it stores string pairs. However, in the NameValueCollection, these are called name / value pairs and there is no requirement to use unique names. This means that multiple values can be stored against a single key. The values can then be looked up using the name and are returned as an array of strings.

6 July 2007