Difference Between Hashtable and Dictionary in C#
By FoxLearn 2/7/2025 3:25:22 AM 74
However, there are some key differences in how they operate and when to use each one.
Creating and Adding Elements to a Hashtable
A Hashtable
can be used to store key-value pairs without specifying the type of key or value. The following example demonstrates how to create a Hashtable
and add some elements to it:
Hashtable studentGrades = new Hashtable(); studentGrades.Add(101, "A"); // Adding key/value pair using Add() method studentGrades.Add(102, "B"); studentGrades.Add(103, "C"); // The following line will throw a runtime exception because the key already exists // studentGrades.Add(103, "A"); foreach (DictionaryEntry entry in studentGrades) { Console.WriteLine("Student ID: {0}, Grade: {1}", entry.Key, entry.Value); }
In this example, the Hashtable
stores student IDs as keys and their corresponding grades as values.
Creating and Adding Elements to a Dictionary
A Dictionary<TKey, TValue>
is a generic collection that requires you to specify the types of both the key and the value.
Dictionary<int, string> studentGrades = new Dictionary<int, string>(); studentGrades.Add(101, "A"); // Adding key/value pair using Add() method studentGrades.Add(102, "B"); studentGrades.Add(103, "C"); // The following line will throw a runtime exception because the key already exists // studentGrades.Add(103, "A"); foreach (KeyValuePair<int, string> pair in studentGrades) { Console.WriteLine("Student ID: {0}, Grade: {1}", pair.Key, pair.Value); }
In this case, the Dictionary
stores student IDs as integers and their grades as strings.
Hashtable vs Dictionary
Below is a comparison table highlighting the differences between Hashtable
and Dictionary
:
Hashtable | Dictionary |
---|---|
Located in the System.Collections namespace. | Located in the System.Collections.Generic namespace. |
It is a non-generic (loosely typed) collection, allowing storage of key-value pairs of any data type. | It is a generic collection, requiring specified data types for keys and values. |
It is thread-safe. | Only public static members are thread-safe in a Dictionary . |
If a key does not exist, it returns null . | If a key does not exist, it throws a KeyNotFoundException . |
Data retrieval is slower due to the need for boxing/unboxing of data types. | Data retrieval is faster because it is type-safe and does not require boxing/unboxing. |
It is recommended to use Dictionary over Hashtable due to better performance and type safety. | More efficient and type-safe compared to Hashtable . |
In conclusion, while both Hashtable
and Dictionary
are used for storing key-value pairs, Dictionary
is generally the preferred choice due to its better performance, type safety, and support for generic types.
- Using the OrderBy and OrderByDescending in LINQ
- Querying with LINQ
- Optimizing Performance with Compiled Queries in LINQ
- MinBy() and MaxBy() Extension Methods in .NET
- SortBy, FilterBy, and CombineBy in NET 9
- Exploring Hybrid Caching in .NET 9.0
- Using Entity Framework with IDbContext in .NET 9.0
- Primitive types in C#