Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDictionary<string, string> versus Dictionary<string, string>

what is the value of using IDictionary here?

like image 699
leora Avatar asked Nov 30 '08 12:11

leora


People also ask

What is the difference between IDictionary and dictionary?

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.

What is an IDictionary?

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 .

What is the difference between Dictionary and Hashtable in C#?

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.

How does dictionary work in C#?

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.


1 Answers

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.

like image 88
Konrad Rudolph Avatar answered Nov 15 '22 17:11

Konrad Rudolph