«

»

Sep 19

.Net Collection Types: Hashtable vs. Dictionary

CSharpCollectionsSmDeciding what type of collection to use in a project is based on the objectives and needs of your project – which you will probably already know.  But to decide which collection object to use, you need to know the major and subtle differences in collection implementations and what their impact will be on your code.  In this analysis, we will look at the System.Collections.Hashtable class versus the System.Collections.Generic.Dictionary class and System.Collections.Concurrent.ConcurrentDictionary class.

All three of these classes implement the System.Collections.IDictionary interface which defines a key/value pair collection.  A Key/Value collection manages two iCollection objects: the Value is the object you are storing in the collection and the Key is a unique identifier associated with a Value.

A hashtable stores collection elements in buckets.  Each of these buckets have a unique hash based on the key of the element.  If you add an element to a hashtable that has the same key as a previously loaded element, the new element will go into the same bucket.  When searching the collection, a hash is provided as the target and only the key/value pairs in the bucket for that hash need to be analyzed.  Because of this, a hashtable can be significantly faster when large datasets are stored in the collection.

Value objects in a hashtable are untyped (an Object) which typically requires boxing and unboxing the value in order to examine it.  Therefore, a Dictionary class with Value of a specific type can provide faster examination of the value entry.

If the collection might be accessed by multiple threads simultaneously, the thread-safe ConcurrentDictionary should be used.

While the performance differences may or may not impact your project; dataset size, whether dataset values are consistent types and whether multiple threads will be accessing the data are important considerations when deciding which collection object to use.

To play around with Hashtables and Dictionaries, here’s a simple snippet.

using System;
using System.Collections;

    class HashTableExample
    {
        public static void Main()
        {
            Hashtable Hasher = new Hashtable();
            // Load some data
            try
            {
                Hasher.Add("Music", "mp3");
                Hasher.Add("Video", "mp4");
                Hasher.Add("Music", "wma");
                Hasher.Add("Document", "doc");
                Hasher.Add("Video", "flv");
            }
            catch 
            {
                Console.WriteLine("A duplicate key attempted to load");
            }
        
            foreach (DictionaryEntry de in Hasher)
            {
                Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
            }
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>