in

Platinum Bay

Peace, Love and Visual Studio Team System

.NETicated

Hashtables and Stacks and LinkedLists Oh My

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)

Published Jun 08 2007, 10:24 AM by Steve
Filed under: ,

Comments

No Comments

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews has been working as a developer for more than 8 years. During this time, he has designed and developed applications in such widely varying areas as trust accounting, medical information management, supply chain management, and retail systems. He has firsthand developer experience with a variety of languages, including Java, VB, and .NET. Most recently, he has been immersed in SharePoint. He is currently employed at RDA Corporation in Philadelphia, PA, as a Software Engineer and a team member in the Architectural Guidance evangelism team. Steve is also an MTCS (x2), ICSOO, and .NET fanatic.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.