HashTable vs Dictionary in C#

HashTable vs Dictionary in C#

Dictionary:

1.       Dictionary throws an exception if we try to find a key which does not exist in current collection.
2.       Dictionary is faster than Hashtable because there is no any boxing and unboxing.
For example:
In the Dictionary, no need boxing and unboxing. i.e.
var employees = new Dictionary<string, Employee>();
Employee emp = employees ["Anil Singh"];                  

In the Hashtable, need to boxing and unboxing. i.e.
var employees = new Hashtable();
Employee emp = customers["Anil Singh"] as Employee;

3.       When we creating dictionary, we must declare the data types of both keys and values.
4.       Dictionary is a generic type and type-safe. i.e.

                        Dictionary<char, int> dic = new Dictionary<char, int>();

Hashtable:

1.       Hashtable returns null values if we try to find a key which does not exist in current collection.
2.       It is slower than dictionary because it requires boxing and unboxing.
For example:
In the Dictionary, no need boxing and unboxing. i.e.
var employees = new Dictionary<string, Employee>();
Employee emp = employees ["Anil Singh"];                  

In the Hashtable, need to boxing and unboxing. i.e.
var employees = new Hashtable();
Employee emp = customers["Ali G"] as Employee;

3.       All the members in a Hashtable are thread safe.
4.       When we creating Hashtable, we no need to declare the data types.
5.       Hashtable is not a generic type and It is loosely-typed data structure.
6.       In Hashtable, we can add any type of keys and values.

Example for Dictionary vs. HashTable as given below.

public class Dictionary
{
    /// <summary>
    /// </summary>
    static void Main(string[] args)
    {
        Dictionary<int, string> dicList = new Dictionary<int, string>();
        dicList.Add(10, "A10");
        dicList.Add(21, "B21");
        dicList.Add(33, "C33");
        foreach (KeyValuePair<int, String> pair in dicList)
        {
            Console.WriteLine(pair.Key + " " + pair.Value);
        }
        Console.ReadLine();
    }
}

The output for dictionary


public class Hashtable_Example
{
    /// <summary>
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
        Hashtable hasList = new Hashtable();
        hasList.Add(1, 100);
        hasList.Add(2, "A2");
        hasList.Add(3, hasList);
        foreach (DictionaryEntry entry in hasList)
        {
            int Key = (int)entry.Key; //Casting
            string value = entry.Value.ToString(); //Casting
            Console.WriteLine((int)entry.Key + " " + entry.Value.ToString());
        }
        Console.ReadLine();
    }
}

The output for HashTable

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

HashTable vs Dictionary in C# HashTable vs Dictionary in C# Reviewed by Anil Singh on 8:26 PM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^