tags:

views:

652

answers:

5

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>();
+7  A: 

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 :)

TheVillageIdiot
+7  A: 

The whole point of interfaces is to provide... well, an interface to whatever module (I use "module" in a broad sense here) so that calling code will not have to worry about how this particular interface is implemented.

As for "How can IDictionary interface be initialized", this is technically not correct. What can be initialized is a variable, whose type is IDictionary<T, V>. Sure enough variables have to be initialized, but that's usually hidden from the "client code".

IDictionary is not very representative, however. Rather, consider an IDataReader interface. You've surely dealt with ADO.NET, so this should look familiar:

public Foo PopulateFromDataReader(SqlDataReader dataReader)

This particular method is tightly coupled to an SqlDataReader, so you'd have to rewrite it for it to support, say, Access or Oracle or MySQL or Firebird or whatever. In other words, you depend on implementation.

Now consider:

public Foo PopulateFromDataReader(IDataReader dataReader)

This method can be used with whatever class that implements IDataReader, which means with basically any ADO.NET-compatible data provider.

Anton Gogolev
+3  A: 

It would be no different to any other interface. Try thinking about a simpler example:

interface IThermometer
{
    double CurrentTemperature { get; }
}

Now we have a way to get the temperature, though we don't care exactly how it's measured. We can create various implementations:

class MercuryThermometer : IThermometer
{
    public double CurrentTemperature
    {
        get { return ... /* gets the temperature somehow */ }
    }
}

The rest of the program doesn't need to know which thermometer it's using.

Daniel Earwicker
+1  A: 

I suspect you've simply overlooked the difference between the variable typed as IDictionary<,> (the interface), and the value (reference) initialized as a Dictionary<,> (note no I; the concrete type).

Marc Gravell
+1  A: 

It's also useful for unit testing. You can write a unit test for a method that accepts an IDictionary instead of a Dictionary and pass a mock. If it were to accept a class instance (which could also be sealed) you'd be a little screwed (you'd have to use the adapter pattern and so on).

Andrei Rinea