Differences between Hash table and Dictionary
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.
Ex
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);
Hash table:
- 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,
Ex
Hashtable hashtable = new Hashtable();
hashtable.Add("Area", 1000);
hashtable.Add("Perimeter", 55);
hashtable.Add("Mortgage", 540);