Dictionary vs Hashtable in C#: Key Differences and Usage
Dictionary is a generic collection providing type safety and better performance, while Hashtable is a non-generic, older collection that stores keys and values as objects. Use Dictionary for type-safe, modern code and Hashtable only for legacy support.Quick Comparison
Here is a quick side-by-side comparison of Dictionary and Hashtable in C#.
| Feature | Dictionary | Hashtable |
|---|---|---|
| Type Safety | Yes, generic types enforce key and value types | No, stores keys and values as objects |
| Namespace | System.Collections.Generic | System.Collections |
| Performance | Faster due to generics and no boxing/unboxing | Slower due to boxing/unboxing |
| Null Keys | Does not allow null keys | Allows one null key |
| Introduced In | .NET Framework 2.0 | .NET Framework 1.0 |
| Usage Recommendation | Preferred for new development | Legacy code support only |
Key Differences
Dictionary<TKey, TValue> is a generic collection introduced in .NET Framework 2.0 that enforces type safety by requiring specific types for keys and values. This means you get compile-time checking and avoid runtime errors related to wrong types. It also improves performance by eliminating the need for boxing and unboxing when storing value types.
On the other hand, Hashtable is a non-generic collection from the early .NET versions. It stores keys and values as object, so you must cast them to the correct type when retrieving. This can cause runtime errors and slower performance due to boxing/unboxing of value types.
Additionally, Hashtable allows one null key, while Dictionary does not allow null keys. The Dictionary class is found in System.Collections.Generic namespace, whereas Hashtable is in System.Collections. Overall, Dictionary is the modern, type-safe, and faster choice for key-value collections.
Code Comparison
Here is how you add and retrieve items using Dictionary<string, int> in C#.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, int> ages = new Dictionary<string, int>(); ages["Alice"] = 30; ages["Bob"] = 25; Console.WriteLine($"Alice's age: {ages["Alice"]}"); Console.WriteLine($"Bob's age: {ages["Bob"]}"); } }
Hashtable Equivalent
Here is the equivalent code using Hashtable. Notice the need for casting when retrieving values.
using System; using System.Collections; class Program { static void Main() { Hashtable ages = new Hashtable(); ages["Alice"] = 30; ages["Bob"] = 25; Console.WriteLine($"Alice's age: {(int)ages["Alice"]}"); Console.WriteLine($"Bob's age: {(int)ages["Bob"]}"); } }
When to Use Which
Choose Dictionary<TKey, TValue> when you want type safety, better performance, and modern code practices. It is the best choice for new development and most scenarios involving key-value pairs.
Use Hashtable only if you are maintaining legacy code that already uses it or if you need to work with APIs that require non-generic collections. Otherwise, avoid Hashtable because it lacks type safety and is slower.