[Knowledge] Differences between Hashtable and Dictionary

0
25

This topic today is talk about Hashtable and Dictionary. We’ll easy to understand and notice that 2 kinds data structure. Ok let’s take a look

Hashtable

In computing, a hash table (hash map) is a data structure which implements an associative array abstract data type, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.

Differences between Hashtable and Dictionary

Dictionary

A dictionary uses a key to reference the value directly inside of an associative array.

In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection.

Differences between Hashtable and Dictionary

Now, what differences between Hashtable and Dictionary

Dictionary:

  • It returns/throws Exception 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 (When creating, must specify the data types for both keys and values).
    Example: Dictionary<string, string> <NameOfDictionaryVar> = new Dictionary<string, string>();
  • Dictionay is a type-safe implementation of Hashtable, Keys and Values are strongly typed.

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,
  • Hashtable is loosely-typed data structure, we can add keys and values of any type.