What is the difference between Hashtable and Dictionary in C#?
Answers (2)
Add AnswerHashtable
- The System includes a hashtable. namespace for collections.
- Since hashtables are weakly typed collections (not generic), they may hold key-value pairs of any data type.
- Hashtable is secure for threads.
- When a key that does not exist is searched for, Hashtable returns null.
- Due to packaging and unpacking, data retrieval takes longer than dictionary searches.
Dictionary
- The Dictionary is a part of System.Collections. universal namespace
- A general group of items is the dictionary. Consequently, it can hold key-value pairs of particular data kinds.
- In Dictionary, only public static members are thread-safe.
- If we try to find a key that doesn’t exist, Dictionary throws an exception.
- Hashtable is slower in terms of data retrieval.