Important Interface in .NET: Work With IDictionary Interface

Welcome to the “Important Interface in C#” article series.  In this series we will be talking about various useful interfaces of the .NET class library. The previous articles explained the IEnumerable, ICollection, IComparable, IList and ISerializable interfaces, you can read them here.

Today’s article explains the IDictonary interface that is a very common and frequently used collection in the .NET class library. I hope most of you are very familiar with Dictionary collections and just for the sake of completeness in this series I have chosen this topic. (Haha.. J) Anyway, this article might help a young developer, those who are still new to .NET collections. (Let’s think your first day of learning about the Dictionary class.)

We know that the working mechanism of a Dictionary collection is very similar to a real-world dictionary. It stores data in a key/value pair. The key part is very crucial and it should be unique in nature in any collection.

Basically a Dictionary stores data as a key and value pair in the form of an object, so we can store any value in the Dictionary data structure.

Ok, let’s start to talk about IDictionary. It is a generic collection that store data in key/value pairs. There are two versions of the IDictionary interface. One is with a parameter and another is parameterless. Here is a small description of them.

IDictionary interface

Namespace:       System.Collection
Assembly    :       mscorlib.dll

IDictionary<TK,TV> interface

This is the parameterized version of the IDictionary interface. The location of the interface in the .NET class library is:

Namespace:       System.Collection.Generic
Assembly:           mscorlib.dll

Here is Visual Studio showing the two versions:

IDictionary interface

Let’s implement a Dictionary in an example. Here the key is an integer type and the value is a string type. Have a look at the following example. Since a Dictionary implements IDictionary, we can initialize an object of IDictionary in a Dictionary. Have a look at the following example.

class Program

{

    static void Main(string[] args)

    {

        IDictionary dis = new Dictionary<int, string>();

        dis.Add(1, "Sourav Kayal");

        dis.Add(2,"Ram kumar");

        Console.WriteLine(dis[1]);

        Console.WriteLine(dis[2]);

        Console.ReadLine();

    }

}

Implement IDictionary 

In the above example we did not mention any parameter the in IDictionary interface but if needed we can pass it as in the following example.

class Program

{

    static void Main(string[] args)

    {

        IDictionary<int, string> dic = new Dictionary<int, string>();

        dic.Add(1, "Facebook");

        dic.Add(2, "Orkut");

        dic.Add(3, "Twitter");

        Console.WriteLine(dic[1]);

        Console.WriteLine(dic[2]);

        Console.WriteLine(dic[3]);

        Console.ReadLine();

    }

}

Here is the output of the example above.

output

The same is true in the case of Dictionary also. In this example we will pass TK and TV parameter types to the Dictionary. Here is a sample implementation.

class Program

{

     static void Main(string[] args)

     {

            Dictionary<int, string> dic = new Dictionary<int, string>();

            dic.Add(1, "C# Book");

            dic.Add(2, "VB Book");

            Console.WriteLine(dic[1]);

            Console.WriteLine(dic[2]);

            Console.ReadLine();

    }

}

parameter type in Dictionary 

Conclusion

This short article has explained the IDictionary and IDictionary<TK,TV> interfaces. Both are used to implement a key/value pair collection in the .NET class library. I hope you have understood the concept. In a future article we will discuss a few more interfaces.


Similar Articles