Search code examples
c#.netidictionary

Purpose of IDictionary interface


What is the need of IDictionary interface. How can IDictionary interface be initialized. After all it is just an interface. The following code snippet is from msdn. I could not understand it.

IDictionary<string, string> openWith = new Dictionary<string, string>();

Solution

  • It defines the important functions that an Dictionary should implement.

    The line from MSDN means that you are creating an object openWith which implements the functions (methods) defined in IDictionary interface.

    When you use Dictionary to declare the variable like:

    Dictionary<string,string> openWith=.....;
    

    you are bind with the concrete type of object. But when you use

    IDictionary<string,string> openWith=....;
    

    you can use it with any object that implements IDictionary interface, maybe your own custom class :)