Click here to Skip to main content
15,880,405 members
Articles / Programming Languages / C#
Tip/Trick

Hashtable vs. Dictionary

Rate me:
Please Sign up or sign in to vote.
4.75/5 (39 votes)
4 Jun 2013CPOL1 min read 273.7K   25   14
A compariosn of Hashtable and Dictionary.

HashTable

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.

Example:

C#
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.
        Console.WriteLine(hashtable.ContainsKey("Perimeter"));
        // Test the Contains method. It works the same way.
        Console.WriteLine(hashtable.Contains("Area"));
        // Get value of Area with indexer.
        int value = (int)hashtable["Area"];
        // Write the value of Area.
        Console.WriteLine(value);
    }
}
Output:
True
True
1000

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.

Example:

C#
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);
           }
           Console.ReadKey();
       }
    }

Differences between Hashtable and Dictionary

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.

Hashtable:

  • 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,

License

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


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood article Pin
Daniel Miller18-Nov-15 7:11
professionalDaniel Miller18-Nov-15 7:11 
AnswerRe: Good article Pin
vaynenick17-Apr-16 21:13
vaynenick17-Apr-16 21:13 
GeneralRe: Good article Pin
Antariksh Verma14-Jun-16 20:21
professionalAntariksh Verma14-Jun-16 20:21 
Thanks Nick Smile | :)
GeneralMy vote of 1 Pin
demeester8-Jun-13 10:40
demeester8-Jun-13 10:40 
GeneralRe: My vote of 1 Pin
richardalgor4-Feb-15 20:49
richardalgor4-Feb-15 20:49 
QuestionWhat was the output for Dictionary Pin
Jamie Munro4-Jun-13 15:29
Jamie Munro4-Jun-13 15:29 
AnswerRe: What was the output for Dictionary Pin
Antariksh Verma4-Jun-13 18:16
professionalAntariksh Verma4-Jun-13 18:16 
GeneralRe: What was the output for Dictionary Pin
John Brett5-Jun-13 2:46
John Brett5-Jun-13 2:46 
GeneralRe: What was the output for Dictionary Pin
Antariksh Verma5-Jun-13 2:56
professionalAntariksh Verma5-Jun-13 2:56 
GeneralRe: What was the output for Dictionary Pin
John Brett5-Jun-13 3:08
John Brett5-Jun-13 3:08 
AnswerRe: What was the output for Dictionary Pin
AmitGajjar23-Sep-13 1:08
professionalAmitGajjar23-Sep-13 1:08 
GeneralRe: What was the output for Dictionary Pin
AmitGajjar23-Sep-13 2:50
professionalAmitGajjar23-Sep-13 2:50 
SuggestionThread Safe Dictionaries Pin
Jamie Munro4-Jun-13 15:28
Jamie Munro4-Jun-13 15: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.