Powered by Blogger.

Tuesday, October 24, 2017

What are the differences between Hashtable and Dictionary in C#.Net?



Here we will discuss what are the differences between Hashtable and Dictionary in C#.Net. Hashtable and Dictionary are collection of data structures to hold data as key-value pairs. Dictionary is generic type, hash table is not a generic type.

A Dictionary class is a data structure that represents a collection of keys and values pair of data. The key is identical in a key-value pair and it can have at most one value in the dictionary, but a value can be associated with many different keys.

The Hashtable is a weakly typed data structure, so you can add keys and values of any Object Type to the Hashtable.

Below are few differences between Hashtable and Dictionary.

Hashtable:
- It returns null if we try to find a key which does not exist.
- It is slower than dictionary because it requires boxing and unboxing.
- All the members in a Hashtable are thread safe,
- Hashtable is not a generic type,

Dictionary:
- It returns error if we try to find a key which does not exist.
- It is faster than a Hashtable because there is no boxing and unboxing.
- Only public static members are thread safe.
- Dictionary is a generic type which means we can use it with any data type.

Hope this will be helpful.





0 comments

Post a Comment