In the ArchDevBrainPick call this morning, a question was asked regarding how to determine which collection type to use.
In .NET, the System.Collections namespace is quite vast and it can be easy to get lost in there. Ok, well maybe not vast, but it does leave you with some design choices. I'll present what classes are available, and then give some tips on which ones to use when. Let's start with an overview of the available options in this namespace and the Specialized namespace. The third column represents the generic equivalents provided to aid in type-safe operations.
Note: some collections were left out, like Queue (Queue<>), Stack (Stack<>), BitVector32 and BitArray.
|
System.Collections |
|
System.Collections.Specialized |
|
System.Collections.Generic (equivalents) |
|
|
ArrayList |
|
List<> |
|
Hashtable |
|
Dictionary<> |
|
SortedList |
|
SortedList<> |
|
DictionaryEntry |
|
NameValuePair<> |
|
ListDictionary |
Dictionary<> |
|
StringCollection |
List<String> |
|
StringDictionary |
Dictionary<String> |
|
NameValueCollection |
Dictionary<> |
|
HybridDictionary |
Dictionary<> |
|
OrderedDictionary |
Dictionary<> |
|
|
LinkedList<> |
Base Collections
ArrayList
A simple, resizable index-based collection of objects.
Generic equivalent: List<>
Hashtable
A collection of name/value pairs of objects that allows retrieval by name or index.
Generic equivalent: Dictionary<>
SortedList
A sorted collection of name/value pairs of objects.
Generic equivalent: SortedList<>
Queue
A first-in, first-out (FIFO) collection of objects.
Generic equivalent: Queue<>
Stack
A last-in, first out (LIFO) collection of objects.
Generic equivalent: Stack<>
Specialized Collections
ListDictionary
Similar to Hashtable, more efficient for smaller collections. Not efficient for larger collections.
Generic equivalent: Dictionary<>
StringCollection
This is a simple dynamically sized collection similar to the ArrayList, but only holds strings.
Generic equivalent: List<String>
StringDictionary
Same interface as the Hashtable, but you can only use strings.
Generic equivalent: Dictionary<String>
NameValueCollection
The NameValueCollection may appear similar to the StringDictionary, but it allows you to store multiple values per key.
Generic equivalent: Dictionary<>
HybridDictionary
Think of it as a cross between the Hashtable and ListDictionary. Useful when you don't know how large your collection will be. It starts out as a ListDictionary. If your collection grows too large, it converts itself under the covers to a Hashtable.
Generic equivalent: Dictionary<>
OrderedDictionary
Same functionality as the Hashtable, but allows you to control the ordering of items through methods such as Insert to an index and RemoveAt.
Generic equivalent: Dictionary<>
ListDictionary
Very efficient for a small collection of objects because it is implemented as a simple array under the covers. Same interface as Hashtable.
Generic equivalent: Dictionary<>
Generic Collections
LinkedList<>
The one generic collection that is not an equivalent of a core collection type is the LinkedList. The idea is a set of items that are linked to each other. From each item you can navigate to sibling items without having to reference the collection itself.
When to Use
To answer the question posed this morning, here are some tips:
Use an ArrayList or StringCollection if you want to store a single collection of objects.
Use a ListDictionary for smaller collections of key/value pairs.
Use a StringDictionary or Hashtable for larger collections of key/value pairs.
Use a HybridDictionary if you don't know ahead of time whether your name/value collection will be large or small.
For more specific uses, check out the Specialized and Generic namespaces.
Hope this helps
Source: My cranium, Reflector (System, mscorlib), and .NET Framework 2.0 Application Development Foundation by Tony Northrup and Shawn Wildermuth (MS Press 2006)