IDictionary
is an interface and Dictionary
is a class.
Dictionary
implements IDictionary
.
That means that this is possible to refer to Dictionary
instance with/by IDictionary
instance and invoke most of the Dictionary
methods and properties through IDictionary
instance.
This is very recommended to use interfaces as many as possible, because interfaces abstracts the modules and assemblies of the applications, allows polymorphism, which is both very common and useful in many situations and cases and allows replacing one module by another without touching the other modules.
Suppose that in the present, the programmer wrote:
IDictionary<string> dictionary = new Dictionary<string>();
And now dictionary
invokes the methods and properties of Dictionary<string>
.
In the future the databases has been grown up in size and we find out that Dictionary<string>
is too slow, so we want to replace Dictionary<string>
by RedBlackTree<string>
, which is faster.
So all what is needed to be done is replacing the above instruction to:
IDictionary<string> dictionary = new RedBlackTree<string>();
Of course that if RedBlackTree
implements IDictionary
then all the code of the application compiles successfully and you have a newer version of your application, where the application now performs faster and more efficient than the previous version.
Without interfaces, this replacement would be more difficult to do and would require the programmers and developers to change more code that is potential to bugs.