What is the difference between a HashTable and Dictionary in C#?

Hashtable is a non-generic collection that stores keys and values as objects. This leads to boxing and unboxing for value types and requires explicit casting when retrieving values — which can hurt performance and cause runtime errors.

Dictionary<TKey, TValue> is the generic, type-safe replacement introduced in .NET 2. It avoids boxing/unboxing, improves performance, and gives compile-time safety. It also integrates better with LINQ and modern .NET features.

I always prefer Dictionary in new codebases. I’ve only worked with Hashtable in maintaining legacy systems.

Comments

Popular posts from this blog

How do you manage performance and memory usage in Entity Framework (EF)?

What are the main OOP Concepts in C#?

What is a REST Service (or REST API)?