By blitzkriegz

2009-05-18 07:55:02 8 Comments

Possible Duplicate:
Why Dictionary is preferred over hashtable in C#?

What is the difference between Dictionary and Hashtable. How to decide which one to use?


@Rashmi Pandit 2009-05-18 08:16:58

The Hashtable class is a specific type of dictionary class that uses an integer value (called a hash) to aid in the storage of its keys. The Hashtable class uses the hash to speed up the searching for a specific key in the collection. Every object in .NET derives from the Object class. This class supports the GetHash method, which returns an integer that uniquely identifies the object. The Hashtable class is a very efficient collection in general. The only issue with the Hashtable class is that it requires a bit of overhead, and for small collections (fewer than ten elements) the overhead can impede performance.

There is Some special difference between two which must be considered:

HashTable: is non-generic collection ,the biggest overhead of this collection is that it does boxing automatically for your values and in order to get your original value you need to perform unboxing , these to decrease your application performance as penalty.

Dictionary: This is generic type of collection where no implicit boxing, so no need to unboxing you will always get your original values which you were stored so it will improve your application performance.

the Second Considerable difference is:

if your were trying to access a value on from hash table on the basis of key that does not exist it will return null.But in the case of Dictionary it will give you KeyNotFoundException.

@Rashmi Pandit 2009-05-18 11:27:04

My comments are from MCTS 2.0 book and its gotten -ve rep ... how ironic!!!! :D

@Marc Gravell 2009-05-18 11:46:44

Not really very ironic... both Hashtable and Dictionary<,> are based on this approach, so it doesn't in any way answer the question of choosing between them.

@blitzkriegz 2009-05-18 12:26:26

The real irony is that I was reading the same MCTS book, got really confused as to which one to prefer, posted it here and got the same text i just read in the form of your answer!!! :) Thanks for replying nyways..

@Rashmi Pandit 2009-05-18 12:40:49

Ha ha ha ha ... we are dealing with lot of ironies (is tht how its spelled) today. Anyways, thanks for clarifying Marc :)

@Marc Gravell 2009-05-18 12:45:26

lol at the meta-irony

@Pranav Singh 2011-07-06 09:50:25

Want to add a difference:

Trying to acess a inexistent key gives runtime error in Dictionary but no problem in hashtable as it returns null instead of error.


       //No strict type declaration
        Hashtable hash = new Hashtable();
        hash.Add(1, "One");
        hash.Add(2, "Two");
        hash.Add(3, "Three");
        hash.Add(4, "Four");
        hash.Add(5, "Five"); 
        hash.Add(6, "Six");
        hash.Add(7, "Seven");
        hash.Add(8, "Eight");
        hash.Add(9, "Nine");
        hash.Add("Ten", 10);// No error as no strict type

        for(int i=0;i<=hash.Count;i++)//=>No error for index 0
            //Can be accessed through indexers
        Console.WriteLine(hash["Ten"]);//=> No error in Has Table

here no error for key 0 & also for key "ten"(note: t is small)

//Strict type declaration
        Dictionary<int,string> dictionary= new Dictionary<int, string>();
        dictionary.Add(1, "One");
        dictionary.Add(2, "Two");
        dictionary.Add(3, "Three");
        dictionary.Add(4, "Four");
        dictionary.Add(5, "Five");
        dictionary.Add(6, "Six");
        dictionary.Add(7, "Seven");
        dictionary.Add(8, "Eight");
        dictionary.Add(9, "Nine");
        //dictionary.Add("Ten", 10);// error as only key, value pair of type int, string can be added

        //for i=0, key doesn't  exist error
        for (int i = 1; i <= dictionary.Count; i++)
            //Can be accessed through indexers
        //Error : The given key was not present in the dictionary.

here error for key 0 & also for key 10 as both are inexistent in dictionary, runtime error, while try to acess.

@Piotr Kula 2014-03-12 09:57:28

I suppose you should be using LINQ to find the FirstOrDefault item if you are trying to work with a collection outside foreach and for.

@Marc Gravell 2009-05-18 07:56:01

Simply, Dictionary<TKey,TValue> is a generic type, allowing:

  • static typing (and compile-time verification)
  • use without boxing

If you are .NET 2.0 or above, you should prefer Dictionary<TKey,TValue> (and the other generic collections)

A subtle but important difference is that Hashtable supports multiple reader threads with a single writer thread, while Dictionary offers no thread safety. If you need thread safety with a generic dictionary, you must implement your own synchronization or (in .NET 4.0) use ConcurrentDictionary<TKey, TValue>.

@t3mujin 2010-07-15 10:38:25

But Dictionary's instance methods aren't thread-safe, unlike the Hashtable

@Pritom Nandy 2011-01-10 11:49:16

Lets give an example that would explain the difference between hashtable and dictionary.

Here is a method that implements hashtable

public void MethodHashTable()
    Hashtable objHashTable = new Hashtable();
    objHashTable.Add(1, 100);    // int
    objHashTable.Add(2.99, 200); // float
    objHashTable.Add('A', 300);  // char
    objHashTable.Add("4", 400);  // string

    lblDisplay1.Text = objHashTable[1].ToString();
    lblDisplay2.Text = objHashTable[2.99].ToString();
    lblDisplay3.Text = objHashTable['A'].ToString();
    lblDisplay4.Text = objHashTable["4"].ToString();

    // ----------- Not Possible for HashTable ----------
    //foreach (KeyValuePair<string, int> pair in objHashTable)
    //    lblDisplay.Text = pair.Value + " " + lblDisplay.Text;

The following is for dictionary

  public void MethodDictionary()
    Dictionary<string, int> dictionary = new Dictionary<string, int>();
    dictionary.Add("cat", 2);
    dictionary.Add("dog", 1);
    dictionary.Add("llama", 0);
    dictionary.Add("iguana", -1);

    //dictionary.Add(1, -2); // Compilation Error

    foreach (KeyValuePair<string, int> pair in dictionary)
        lblDisplay.Text = pair.Value + " " + lblDisplay.Text;

@nawfal 2011-07-08 18:46:05


@USER_NAME 2013-03-20 06:59:37

Super and simple

@Kenta 2013-12-10 15:05:20

Nice answer .Its always better to explain through examples than writting theory .

@jedd.ahyoung 2014-05-30 15:40:00

As a can enumerate over Hashtable properties, as described here:…. You have to use a DictionaryEntry as your variable, which can then provide key and value objects.

@Manoj Weerasooriya 2014-12-18 04:30:55

Superb answer,Also you can use DictionaryEntry for enumerate over Hashtable.Ex: foreach(DictionaryEntry dnty in objHashTable ){lblDisplay.Text= dnty.Value + " " +dnty.Key}

@Mike 2018-10-09 03:03:17

//Looping hastable foreach (DictionaryEntry entry in objHashTable ) { Console.WriteLine("{0}, {1}", entry.Key, entry.Value); }

@shiv govind 2010-07-04 20:02:04

ILookup Interface is used in .net 3.5 with linq.

The HashTable is the base class that is weakly type; the DictionaryBase abstract class is stronly typed and uses internally a HashTable.

I found a a strange thing about Dictionary, when we add the multiple entries in Dictionary, the order in which the entries are added is maintained. Thus if I apply a foreach on the Dictionary, I will get the records in the same order I have inserted them.

Whereas, this is not true with normal HashTable, as when I add same records in Hashtable the order is not maintained. As far as my knowledge goes, Dictionary is based on Hashtable, if this is true, why my Dictionary maintains the order but HashTable does not?

As to why they behave differently, it's because Generic Dictionary implements a hashtable, but is not based on System.Collections.Hashtable. The Generic Dictionary implementation is based on allocating key-value-pairs from a list. These are then indexed with the hashtable buckets for random access, but when it returns an enumerator, it just walks the list in sequential order - which will be the order of insertion as long as entries are not re-used.

shiv govind Birlasoft.:)

@Rohit Gupta 2010-03-16 03:04:45

There is one more important difference between a HashTable and Dictionary. If you use indexers to get a value out of a HashTable, the HashTable will successfully return null for a non-existent item, whereas the Dictionary will throw an error if you try accessing a item using a indexer which does not exist in the Dictionary

@Frans Bouma 2009-05-18 07:57:00

Dictionary is typed (so valuetypes don't need boxing), a Hashtable isn't (so valuetypes need boxing). Hashtable has a nicer way of obtaining a value than dictionary IMHO, because it always knows the value is an object. Though if you're using .NET 3.5, it's easy to write an extension method for dictionary to get similar behavior.

If you need multiple values per key, check out my sourcecode of MultiValueDictionary here: multimap in .NET

@Marc Gravell 2009-05-18 08:01:49

For multiple values per key: in .NET 3.5, you might also consider implementing ILookup<TKey,TValue> (which is the multi-map interface). Unfortunately the default concrete implementation is immutable, but it is easy to re-implement (or add to your MultiValueDictionary). There is a simple example of such in MiscUtil (EditableLookup<TKey,TValue>)

@Frans Bouma 2009-05-18 08:08:46

Good tip indeed, I had forgotten about that interface. I looked at the implementation in the BCL but it's indeed immutable so pretty much useless for every-day multi-value usage ;). I'll add the interface.

@Frans Bouma 2009-05-18 09:06:37

@Frans Bouma 2009-05-18 10:10:18

Hmm, there's a problem with ILookup<>: it also implements an Enumerator, which is different from the Dictionary enumerator. When a Linq operator is used on the multivaluedictionary, it can't chose which enumerator to use as it also can use IEnumerable<TKey, IGrouping<TKey, TValue>>, even though ILookup is implemented explicitly... Which makes it harder to use the dictionary as it requires explicit type specfication.

@Dmitri Nesteruk 2010-08-17 11:08:22

MultiDictionary is also present in PowerCollections:

Related Questions

Sponsored Content

61 Answered Questions

[SOLVED] What is the difference between String and string in C#?

32 Answered Questions

[SOLVED] What is the difference between const and readonly?

27 Answered Questions

[SOLVED] What is the best way to iterate over a dictionary?

  • 2008-09-26 18:20:06
  • Jake Stewart
  • 1352656 View
  • 2250 Score
  • 27 Answer
  • Tags:   c# dictionary loops

34 Answered Questions

[SOLVED] Differences between HashMap and Hashtable?

19 Answered Questions

[SOLVED] Why is Dictionary preferred over Hashtable in C#?

26 Answered Questions

15 Answered Questions

[SOLVED] Returning IEnumerable<T> vs. IQueryable<T>

38 Answered Questions

23 Answered Questions

[SOLVED] What's the difference between the 'ref' and 'out' keywords?

6 Answered Questions

[SOLVED] \d is less efficient than [0-9]

Sponsored Content