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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
http://www.code-sample.com
http://www.code-sample.xyz

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.