what is the value of using IDictionary here?
the difference is that Dictionary is concrete implementation while IDictionary is just a contract, abstraction. It is recommended for example to expect as argument an IDictionary rather that concrete Dictionary, or to expose property of IDictionary rather that Dictionary, because this promotes loose coupling.
The IDictionary<TKey,TValue> interface is the base interface for generic collections of key/value pairs. Each element is a key/value pair stored in a KeyValuePair<TKey,TValue> object. Each pair must have a unique key. Implementations can vary in whether they allow key to be null .
In Hashtable, you can store key/value pairs of the same type or of the different type. In Dictionary, you can store key/value pairs of same type. In Hashtable, there is no need to specify the type of the key and value. In Dictionary, you must specify the type of key and value.
A dictionary, also called an associative array, is a collection of unique keys and a collection of values, where each key is associated with one value. Retrieving and adding values is very fast. Dictionaries take more memory because for each value there is also a key.
The value of using an interface is always the same: you don't have to change client code when switching to another backend implementation.
Consider that profiling your code later shows that a hash table implementation (used in the Dictionary
class) isn't suited for your task and that a binary search tree would perform better. If you've coded to an interface then switching the implementation is straightforward. If, however, you've used a concrete class, you've got to change a lot more code in a lot more places. => This costs time and money.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With