2
Reply

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

    The Difference between Dictionary and Hash table can be represented as follows:

    1.Dictionary is a generic type whereas Hashtable is not.
       Dictionary is a generic type means 
    a)We get type safety with dictionary ,bcoz we can't insert any random object into it and don't have to cast the value when take out whereas in hashtable we can dynamically  add or remove elements without indexing.
    b)Generic collection is faster as there is no boxing or unboxing here.
    c)Hashtable have to box/unbox so again there is memory consumption as well as performance penalties.
    d)Hash table uses object to hold the things internally(only non generic way to do it).
    2.Hashtable is a collection of name/value pairs that are organised on the basis of hash code of the key being specified. whereas  In Dictionary we need to specify the types of both the key and the corresponding value.the value represents the actual object stored and the key represents a means to identify a particular object.
    3 Hashtable is thread safe for use by multiple reader threads and a single writing thread whereas in Dictionary public static members are type safe but any instance members are not type safe.
    4.We cannot use Dictionary with Web Services The reason is no web service standard supports generic standard.


    Dictionary is a strongly typed generic collection while hashtable collection takes a object datatype. As dictionary is strongly typed so there is no boxing unboxing while in hashtable due to object datatype there is boxing and unboxing. So dictionaries are faster than hashtable.

    Regards,
    See my 20 important c# interview questions and  answers