articles

Home / DeveloperSection / Articles / Hashtable And Dictionary in C#

Hashtable And Dictionary in C#

Anonymous User8874 22-Sep-2014

In this article I’m explaining about Hashtable VS Dictionary in C#.

if you learn more  about Create and Manage Thread read my previous post  Creating, Managing and Destroying Threads in C#

Hashtable:

The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.

A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.

Example


using System;
using System.Collections;
 
namespace ExHashtable
{
    classProgram
    {
        staticHashtable GetHashtable()
        {
            Hashtable hashtable = newHashtable();
            hashtable.Add("Kamlakar", "jaisalmer");
            hashtable.Add("Rohit", "Allahabad");
            hashtable.Add("Pawan", "Goa");
            return hashtable;
        }
        publicstaticvoid Main()
        {
            Hashtable hashtable = GetHashtable();
            string add1 = (string)hashtable["Kamlakar"];
            Console.WriteLine(add1);
            string add2 = (string)hashtable["Rohit"];
            Console.WriteLine(add2);
            string add3 = (string)hashtable["Pawan"];
            Console.WriteLine(add3);
        }
    }
}
Output


Hashtable And Dictionary in C#

 

Dictionary:

A dictionary is used where fast lookups are critical. The Dictionary type provides fast lookups with keys to get values. With it we use keys and values of any type, including ints and strings. Dictionary requires a special syntax form.

Dictionary is used when we have many different elements. We specify its key type and its value type. It provides good performance.

Creating a Dictionary

The Dictionary class is a generic class and can store any data types. This class is defined in the System.Collections.Generic namespace. Before you use a Dictionary class in your code, you must import the System.Collections.Generic namespace using the following line.

using System.Collections.Generic;

The Dictionary class constructor takes two parameters (generic type), first for the type of the key and second for the type of the value. The following code snippet creates a Dictionary where keys are strings and values are short. 

Dictionary<stringInt16> AuthorList = new Dictionary<stringInt16>();z

Example
using System;
using System.Collections;
 
namespace ExHashtable
{
    classProgram
    {
        staticvoid Main()
        {
            Dictionary<string, Int16> EmployeeList = newDictionary<string, Int16>();
 
            EmployeeList.Add("Rohit Kesharwani",26);
            EmployeeList.Add("Sumit Kesarwani",35);
            EmployeeList.Add("Pawan Shukla",24);
            EmployeeList.Add("Sunil Kumar", 25);
            EmployeeList.Add("Kamlakar Singh",22);
            EmployeeList.Add("Manoj Pandey",23);
            EmployeeList.Add("Anchal Kesahrwani", 24);
 
            // Read all data
            Console.WriteLine("Employee List");
 
            foreach (KeyValuePair<string, Int16> employee in EmployeeList)
            {
                Console.WriteLine("Key = {0},Value = {1}", employee.Key,employee.Value);
            }
 
            Console.ReadKey();
        }
    }
}

 

Output

Hashtable And Dictionary in C#

 

Properties

Count: The count property gets the number of key/value pairs in dictionary. The fallowing code display the number of items in dictionary.

Console.WriteLine("Count:  {0}",EmployeeList.Count);

Output

Count: 7 

Differences between Hashtable and Dictionary

 

Dictionary

  1. It returns error if we try to find a key which does not exist.
  2. It is faster than a Hashtable because there is no boxing and unboxing.
  3. Only public static members are thread safe.
  4. Dictionary is a generic type which means we can use it with any data type.
Hashtable


  1. It returns null if we try to find a key which does not exist.
  2. It is slower than dictionary because it requires boxing and unboxing.
  3. All the members in a Hashtable are thread safe,
  4. Hashtable is not a generic type, 

 in my next post, i'll explain about Backbone.js


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By