In C#, Hashtable and Dictionary are used to store key-value pairs, but they have some differences in terms of functionality, implementation and features,
Dictionary
There are several points in Dictionary that make it different from Hashtable which are as follows,
Namespace- Dictionary is present in the System.Collections.Generic namespace.
Type Safety- The Dictionary is type-safe, you must specify the
data type for both the keys and the values
when you declare it.
Performance-Dictionary usually provides better performance than
Hashtable since it is a generic collection and avoids boxing/unboxing overhead
Null Keys- the Dictionary does not allow the
null key but allows null values.
Enumeration- Enumerating over a dictionary with
foreach is more efficient than enumerating over a hashtable.
Enumeration Order-Dictionary does not guarantee the order of the elements while
Hashtable also does not preserve the order of the elements.
Obsolete- The dictionary is a newer and preferred alternative to
key-value pairs in most cases.
Example-
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Creating a Dictionary
Dictionary<string, string> dictionary = new Dictionary<string, string>();
// Adding key-value pairs to the Dictionary
dictionary.Add("1", "One");
dictionary.Add("2", "Two");
dictionary.Add("3", "Three");
// Accessing elements in the Dictionary
Console.WriteLine("Value at key '1': " + dictionary["1"]);
Console.WriteLine("Value at key '2': " + dictionary["2"]);
Console.WriteLine("Value at key '3': " + dictionary["3"]);
// Iterate over dictionary
Console.WriteLine("\n Iterate over dictionary:");
foreach (KeyValuePair<string, string> pair in dictionary)
{
Console.WriteLine("Key: " + pair.Key + ", Value: " + pair.Value);
}
Console.ReadLine();
}
}
Output-
Value at key '1': One
Value at key '2': Two
Value at key '3': Three
Iterate over dictionary:
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three
Hashtable
There are also some points in Hashtable that differ from Dictionary which are as follow,
Namespace- Dictionary is present in the System.Collections namespace.
Type Safety- The Hashtable is not type-safe, because it allows any
datatype for both key and value.
Performance-Hashtable uses a simple hash code
algorithm that's why its performance is not as good as Dictionary
Null Keys- The Hashtable allows both the
null key and null value
Enumeration- Hashtable needs boxing/unboxing, which can degrade performance during calculation.
Enumeration Order- The Hashtable doesn't preserve the order of the elements.
Obsolete- The Hashtable is a part of an older non-generic collection framework in C#
Example-
using System;
using System.Collections;
class Program
{
static void Main()
{
// Creating a Hashtable
Hashtable hashtable = new Hashtable();
// Adding key-value pairs to the Hashtable
hashtable.Add("1", "One");
hashtable.Add("2", "Two");
hashtable.Add("3", "Three");
// Accessing elements in the Hashtable
Console.WriteLine("Value at key '1': " + hashtable["1"]);
Console.WriteLine("Value at key '2': " + hashtable["2"]);
Console.WriteLine("Value at key '3': " + hashtable["3"]);
// Iterating through the Hashtable
Console.WriteLine("\nIterating through the Hashtable:");
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("Key: " + entry.Key + ", Value: " + entry.Value);
}
}
}
Output-
Value at key '1': One
Value at key '2': Two
Value at key '3': Three
Iterating over the Hashtable:
Key: 2, Value: Two
Key: 3, Value: Three
Key: 1, Value: One
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Dictionary vs Hashtable in C#
In C#, Hashtable and Dictionary are used to store key-value pairs, but they have some differences in terms of functionality, implementation and features,
Dictionary
There are several points in Dictionary that make it different from Hashtable which are as follows,
Namespace- Dictionary is present in the
System.Collections.Genericnamespace.Type Safety- The
Dictionaryis type-safe, you must specify the data type for both the keys and the values when you declare it.Performance-
Dictionaryusually provides better performance thanHashtablesince it is a generic collection and avoids boxing/unboxing overheadNull Keys- the
Dictionarydoes not allow the null key but allows null values.Enumeration- Enumerating over a
dictionarywith foreach is more efficient than enumerating over ahashtable.Enumeration Order-
Dictionarydoes not guarantee the order of the elements whileHashtablealso does not preserve the order of the elements.Obsolete- The
dictionaryis a newer and preferred alternative to key-value pairs in most cases.Example-
Output-
Hashtable
There are also some points in Hashtable that differ from Dictionary which are as follow,
Namespace- Dictionary is present in the
System.Collectionsnamespace.Type Safety- The
Hashtableis not type-safe, because it allows any datatype for both key and value.Performance-
Hashtableuses a simple hash code algorithm that's why its performance is not as good as DictionaryNull Keys- The
Hashtableallows both the null key and null valueEnumeration- Hashtable needs boxing/unboxing, which can degrade performance during calculation.
Enumeration Order- The
Hashtabledoesn't preserve the order of the elements.Obsolete- The Hashtable is a part of an older non-generic collection framework in C#
Example-
Output-
Also, Read: Pascal Triangle