views:

1600

answers:

3

what is the value of using IDictionary here?

+11  A: 

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.

Konrad Rudolph
+8  A: 

IDictionary enables looser coupling.

Say you have a method like this:

void DoSomething(IDictionary<string, string> d)
{
   //...
}

You can use it like this:

Dictionary<string, string> a = new Dictionary<string, string>();
SortedDictionary<string, string> b = new SortedDictionary<string, string>();
DoSomething(a);
DoSomething(b);
Arjan Einbu
A: 

Based on your previous question Class inherits generic dictionary<string, IFoo> and Interface and the discussion in the answers, I'm guessing your asking more about inheriting from rather than using.

Implementing from Dictionary is fine if your happy with the way dictionary is implemented. If you want to change something, then implementing IDictionary is better.

IMHO if you're going to cover an existing member with 'new' then it's better to implement the interface rather than inherit from the original class.

Cameron MacFarland