python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions