Click here to Skip to main content
12,703,063 members (30,017 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


24 bookmarked

Hashtable vs. Dictionary

, 4 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
A compariosn of Hashtable and Dictionary.


Hashtable optimizes lookups. It computes a hash of each key you add. It then uses this hash code to look up the element very quickly. It is an older .NET Framework type. It is slower than the generic Dictionary type.


using System.Collections;
class Program
    static Hashtable GetHashtable()
        // Create and return new Hashtable.
        Hashtable hashtable = new Hashtable();
        hashtable.Add("Area", 1000);
        hashtable.Add("Perimeter", 55);
        hashtable.Add("Mortgage", 540);
        return hashtable;
    public  static void Main()
        Hashtable hashtable = GetHashtable();
        // See if the Hashtable contains this key.
        // Test the Contains method. It works the same way.
        // Get value of Area with indexer.
        int value = (int)hashtable["Area"];
        // Write the value of Area.


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.


class Dict
    static void Main()
       // Example Dictionary again
       Dictionary<string, int> d = new Dictionary<string, int>()
           {"Lion", 2},  {"dog", 1}};
           // Loop over pairs with foreach
           foreach (KeyValuePair<string, int> pair in d)
               Console.WriteLine ("{0}, {1}",pair.Key,  pair.Value);
           foreach (var pair in d)
               Console.WriteLine("{0}, {1}", pair.Key, pair.Value);

Differences between Hashtable and Dictionary


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


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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Antariksh Verma
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionGood article Pin
Daniel Miller18-Nov-15 8:11
professionalDaniel Miller18-Nov-15 8:11 
AnswerRe: Good article Pin
vaynenick17-Apr-16 22:13
membervaynenick17-Apr-16 22:13 
GeneralRe: Good article Pin
Antariksh Verma14-Jun-16 21:21
professionalAntariksh Verma14-Jun-16 21:21 
Questionwonderful post Pin
Member 1152781420-Mar-15 5:31
memberMember 1152781420-Mar-15 5:31 
GeneralMy vote of 1 Pin
demeester8-Jun-13 11:40
memberdemeester8-Jun-13 11:40 
GeneralRe: My vote of 1 Pin
richardalgor4-Feb-15 21:49
memberrichardalgor4-Feb-15 21:49 
QuestionWhat was the output for Dictionary Pin
Jamie Munro4-Jun-13 16:29
memberJamie Munro4-Jun-13 16:29 
AnswerRe: What was the output for Dictionary Pin
Member 99723584-Jun-13 19:16
memberMember 99723584-Jun-13 19:16 
GeneralRe: What was the output for Dictionary Pin
John Brett5-Jun-13 3:46
memberJohn Brett5-Jun-13 3:46 
GeneralRe: What was the output for Dictionary Pin
Antariksh Verma5-Jun-13 3:56
professionalAntariksh Verma5-Jun-13 3:56 
GeneralRe: What was the output for Dictionary Pin
John Brett5-Jun-13 4:08
memberJohn Brett5-Jun-13 4:08 
AnswerRe: What was the output for Dictionary Pin
@AmitGajjar23-Sep-13 2:08
professional@AmitGajjar23-Sep-13 2:08 
GeneralRe: What was the output for Dictionary Pin
@AmitGajjar23-Sep-13 3:50
professional@AmitGajjar23-Sep-13 3:50 
SuggestionThread Safe Dictionaries Pin
Jamie Munro4-Jun-13 16:28
memberJamie Munro4-Jun-13 16:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 4 Jun 2013
Article Copyright 2013 by Antariksh Verma
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid