What is the difference between Hashtable and Dictionary in C#?

Forums ASP.NET MVCWhat is the difference between Hashtable and Dictionary in C#?
Staff asked 2 years ago

Answers (2)

Add Answer
prinkesha lathidadiya Marked As Accepted
Staff answered 2 years ago
Hashtable
  • 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.
Staff answered 2 years ago

Hashtables allow you to store key/value pairs of either a different type or the same kind.
You can store key/value pairs of the same type in a dictionary.

Subscribe

Select Categories