0
0
CsharpComparisonBeginner · 4 min read

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#.

FeatureDictionaryHashtable
Type SafetyYes, generic types enforce key and value typesNo, stores keys and values as objects
NamespaceSystem.Collections.GenericSystem.Collections
PerformanceFaster due to generics and no boxing/unboxingSlower due to boxing/unboxing
Null KeysDoes not allow null keysAllows one null key
Introduced In.NET Framework 2.0.NET Framework 1.0
Usage RecommendationPreferred for new developmentLegacy 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#.

csharp
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"]}");
    }
}
Output
Alice's age: 30 Bob's age: 25
↔️

Hashtable Equivalent

Here is the equivalent code using Hashtable. Notice the need for casting when retrieving values.

csharp
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"]}");
    }
}
Output
Alice's age: 30 Bob's age: 25
🎯

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.

Key Takeaways

Dictionary is type-safe and faster than Hashtable.
Hashtable stores keys and values as objects, requiring casting and causing slower performance.
Dictionary does not allow null keys; Hashtable allows one null key.
Use Dictionary for new code and Hashtable only for legacy support.
Dictionary is in System.Collections.Generic; Hashtable is in System.Collections.