---
title: "Hashtable And Dictionary in C#"  
description: "In this article I’m explaining about Hashtable VS Dictionary in C#."  
author: "Anonymous User"  
published: 2014-09-22  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1500/hashtable-and-dictionary-in-c-sharp  
category: "asp.net"  
tags: ["c#", ".net", "hashtable"]  
reading_time: 3 minutes  

---

# Hashtable And Dictionary in C#

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{    class Program    {        static Hashtable GetHashtable()        {            Hashtable hashtable = new Hashtable();            hashtable.Add("Kamlakar", "jaisalmer");            hashtable.Add("Rohit", "Allahabad");            hashtable.Add("Pawan", "Goa");            return hashtable;        }        public static void 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#](https://www.mindstick.com/mindstickarticle/cdc0afac-363b-4b2a-b480-12a5199a4ff8/images/87201705-d570-46d8-9d9d-c33443bf661d.png)

##### 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<string, Int16> AuthorList = new Dictionary<string, Int16>();z

##### Example

```
using System;using System.Collections; namespace ExHashtable{    class Program    {        static void Main()        {            Dictionary<string, Int16> EmployeeList = new Dictionary<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#](https://www.mindstick.com/mindstickarticle/cdc0afac-363b-4b2a-b480-12a5199a4ff8/images/71b8cf6b-c1e1-48db-8c8c-1118b0fceb54.png)

##### 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\

---

Original Source: https://www.mindstick.com/articles/1500/hashtable-and-dictionary-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
