Why Dictionary is preferred over hashtable in C#?

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

 

 

 Dictionary is typed (so valuetypes don't need boxing), a Hashtable isn't (so valuetypes need boxing).

 

The Hashtable class uses the hash to speed up the searching for a specific key in the collection.

 

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

 

When we want a collection  data structure to hold data as key/value pairs, we have two obvious choices.

1. Dictionary

2. Hashtable

So basically what is the difference between both this and when to use which. Lets check it out. Point by Point.

1) Generics

Dictionary is a generic type, Hashtable is not. Now what that means. This is the most important aspect one should consider when taking decision. Dictionary is a generic type means

- You get type safety with Dictionary, because you can't insert any random object into it, and you don't have to cast the values you take out.

- And also generic collections are a lot faster as there's no boxing/unboxing

- Hashtable uses Object to hold things internally (Only non-generic way to do it). So you have to take care of type safety and cast it to appropriate data type when you take it out, but it also means that you can add keys and values of any type ( It is good or bad, well it depends :) )

- Again Hashtable also have to box/unbox, which may have memory consumption as well as performance penalties.

2) Thread Safety :

In .Net Hashtable is thread safe for use by multiple reader threads and a single writing thread, while in Dictionary public static members are thread safe, but any instance members are not guaranteed to be thread safe.

One more thing that can make difference is,  we can not use dictionary (generics) with web services. The reason is no web service standard supports generics standard.

 

Technorati Tags: ,,,,,,,,

 

Windows Live Tags: C#,Hashtable,Dictionary,collection,Generics,instance,services,supports,collections

 

 

WordPress Tags: C#,Hashtable,Dictionary,collection,Generics,instance,services,supports,collections

 

 

 

 

 

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.

e.g.

//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[i]); } 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 Console.WriteLine(dictionary[i]); } //Error : The given key was not present in the dictionary. //Console.WriteLine(dictionary[10]);

 

 

 

Comments