a blog for those who code

Wednesday 25 June 2014

Dictionary Vs Hashtable

In this post we will show you the difference between Dictionary and Hashtable. Differences between Dictionary and Hashtable are as follows...


1. Dictionary are generic type i.e. we can use any datatype to initialize the dictionary with, but Hastable are not generic type.

2. Dictionary throws error if you try to find a key which does not exist whereas Hastable doesn't throw error instead it returns null.

3. Dictionary does not require to do boxing and unboxing whereas hashtable requires boxing and unboxing. Because of this dictionary is bit faster than hashtable.

4. Only public static members of dictionary are thread safe but all the members of hashtable are thread safe.

5. Dictionary relies on chaining whereas Hashtable relies on rehashing.

6. Dictionary maintains an order of entries when they are added but Hastable doesnot maintain that order.

So, it is upto you which to use based on your requirement. Dictionary is relatively newer to .Net than Hashtable. To use Dictionary you have to refer to System.Collections.Generic whereas to use Hastable you have to refer to System.Collections.

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment