A hash table is used when a key is needed to access an element, and useful key values can be identified. Each item in the hash table has a key/value pair. Keys are used to access items in the collection.
Members in the hash table are thread-safe. If we try to find a key that doesn't exist, it will return null. Hashtable is not a generic type.
Hashtable collection is slower than dictionary because it requires boxing and unboxing.
Declaration Hashtable -
Hashtable ht = new Hashtable();
A dictionary is a collection of keys and values in C#. Dictionary is contained in the System.Collection.Generics namespace. Dictionary is a generic type and will return an error if you try to find a key that doesn't exist.
Dictionary collections are faster than Hashtables because there is no boxing and unboxing.
Declare a dictionary -
IDictionary<int, string> d = new Dictionary<int, string>();
The above is the detailed content of Hash tables and dictionaries in C#. For more information, please follow other related articles on the PHP Chinese website!