4
Reply

What is the difference between Dictionary and Hashtable ?

Murali Poola

Murali Poola

12y
18.3k
0
Reply

    Dictionary is a Generic Type where as HashTable is not Both Dictionary & HashTable holds the data like key, value as

    Dictionary and Hash table are collection of data structure to hold data as key-value pairs. Dictionary is generic type, hash table is not generic type. We can't use dictionary with web services. In .NET hash table is thread safe for use by multiple reader thread 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.

    HashTable takes different datatypes as collection, but Dictionary allows only Specified datatype through out collection.

    Dictionary 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 (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.

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