A difference in style IDictionary vs Dictionary
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In the .NET ecosystem, dictionaries are a fundamental collection type, providing an efficient way to map unique keys to values. Among the constructs available in C#, `IDictionary` and `Dictionary` are frequently used to manage these key-value pairs. This article explores the differences between these two interfaces and classes, delving into when and why you might prefer one over the other to achieve optimal performance and code clarity.
Understanding `IDictionary`
`IDictionary` is an interface that represents a collection of key-value pairs. It is defined in the `System.Collections` namespace. As an interface, `IDictionary` provides a contract for any dictionary-like collection, ensuring that certain methods and properties are implemented, such as:
- Contains: Determines whether the dictionary contains a specific key.
- Add: Adds a key-value pair to the collection.
- Remove: Removes a key-value pair by key.
- Count: Provides the number of key-value pairs.
Although `IDictionary` lays out these methods, it lacks implementation details, allowing for flexibility and customization by developers who implement this interface or use a class derived from it.
Example Interface Implementation
Below is a simplistic example of a custom class implementing `IDictionary`.
- Type Safety: Since it is generic, a `Dictionary<TKey, TValue>` offers compile-time type checking, thus avoiding casting and boxing.
- Efficiency: It uses a hash table for storage, giving average time complexity for lookups and modifications as O(1), under ideal conditions.
- Versatile: Supports a variety of keys and values, as long as the keys are unique and hashable.
- Flexibility: If you need to maintain flexibility or provide a custom collection with specialized behaviors, then implementing `IDictionary` might be the right choice.
- Performance: For most applications where performance and rapid lookups/insertions are critical, `Dictionary<TKey, TValue>` would typically be preferred due to its optimized nature.
- Type Safety: Whenever possible, using `Dictionary<TKey, TValue>` is beneficial since it avoids the pitfalls of runtime errors associated with typecasting in non-generic collections.
Related reading
- A divide-and-conquer algorithm for counting dominating points?
- A fast algorithm for minimum spanning trees when edge lengths are constrained?
- A fast array shift implementation in C?
- A Java collection of value pairs? (tuples?)
- A fatal error occurred. The folder /usr/share/dotnet/host/fxr does not exist
- A property or indexer may not be passed as an out or ref parameter
- A KeyValuePair in Java
- A Memory-Adaptive Merge Algorithm?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.