Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Why is a Dictionary preferred over a Hashtable in C#?
In C#, Dictionary is generally preferred over Hashtable due to its better performance, type safety, and modern design. Both are key-value collections, but Dictionary provides significant advantages for most applications.
A Hashtable is a non-generic collection that stores key-value pairs as object types, requiring boxing and unboxing operations. A Dictionary<TKey, TValue> is a generic collection from the System.Collections.Generic namespace that provides compile-time type safety and better performance.
Key Differences
| Feature | Hashtable | Dictionary |
|---|---|---|
| Type Safety | No compile-time type checking | Strong type checking at compile-time |
| Performance | Slower due to boxing/unboxing | Faster, no boxing for value types |
| Null Keys | Does not allow null keys | Allows one null key (if TKey allows it) |
| Thread Safety | Thread-safe for multiple readers | Not thread-safe by default |
Performance Comparison
Dictionary performs better than Hashtable for strongly-typed collections because it avoids boxing and unboxing operations −
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
class Program {
public static void Main() {
const int iterations = 1000000;
// Hashtable performance test
Hashtable hashtable = new Hashtable();
for (int i = 0; i dictionary = new Dictionary();
for (int i = 0; i
The output of the above code shows Dictionary's performance advantage −
Hashtable time: 156ms
Dictionary time: 47ms
Dictionary is 3.32x faster
Type Safety Example
Dictionary provides compile-time type checking, preventing runtime errors −
using System;
using System.Collections;
using System.Collections.Generic;
class Program {
public static void Main() {
// Hashtable - no type safety
Hashtable hashtable = new Hashtable();
hashtable["name"] = "John";
hashtable["age"] = 25;
hashtable["invalid"] = new DateTime(); // Can add any type
// Runtime casting required - potential for errors
string name = (string)hashtable["name"];
int age = (int)hashtable["age"];
Console.WriteLine($"Hashtable: {name}, {age}");
// Dictionary - type safe
Dictionary dictionary = new Dictionary();
dictionary["name"] = "Jane";
dictionary["age"] = 30;
// Type safety at compile time
Console.WriteLine($"Dictionary: {dictionary["name"]}, {dictionary["age"]}");
// Strongly typed dictionary
Dictionary scores = new Dictionary();
scores["Math"] = 95;
scores["Science"] = 87;
// scores["English"] = "A+"; // Compile-time error
Console.WriteLine($"Math Score: {scores["Math"]}");
}
}
The output of the above code is −
Hashtable: John, 25
Dictionary: Jane, 30
Math Score: 95
Modern Usage Example
Dictionary provides better syntax and functionality for modern C# applications −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
// Dictionary with collection initializer
Dictionary studentGrades = new Dictionary {
{"Alice", 92},
{"Bob", 85},
{"Charlie", 78}
};
// Safe key access with TryGetValue
if (studentGrades.TryGetValue("Alice", out int grade)) {
Console.WriteLine($"Alice's grade: {grade}");
}
// Modern iteration
foreach (var kvp in studentGrades) {
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
// LINQ support
var topStudents = studentGrades.Where(x => x.Value >= 85).ToList();
Console.WriteLine($"Top students count: {topStudents.Count}");
}
}
The output of the above code is −
Alice's grade: 92
Alice: 92
Bob: 85
Charlie: 78
Top students count: 2
Conclusion
Dictionary is preferred over Hashtable in C# because it provides better performance through type safety, eliminates boxing/unboxing overhead, and offers modern features like collection initializers and LINQ support. Use Dictionary for new applications unless you specifically need Hashtable's thread-safety characteristics.
