Understand Dictionary and IDictionary in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 26682 red flag

How to implement Dictionary and IDictionary in C#

Understand Dictionary and IDictionary in C#

In this article we will try to learn Dictionary and IDictinary in C# programming language. Both belong to collection class in .NET class library. They store data in Key and Value pair.

Dictionary class in C#

As mentioned earlier data is stored in key value pair. It takes any type as key and value. In visual studio we can check it.


In below example we will create simple dictionary with integer as key and string data as value. Have a look on below code.

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //Understand Dictinary
            Dictionary<int, string> Name = new Dictionary<int, string>();
            Name.Add(1, "sourav");
            Name.Add(2, "Ram");
            Name.Add(3, "shyam");
            foreach(String name in Name.Values)
            {
                Console.WriteLine(name);
            }
   
            Console.ReadLine();
        }
    }
}

Here is output.


Remove item from Dictionary

We can remove any item by specifying it’s key value. In this example we will use Remove() method to remove one item from Dictionary collection.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //Understand Dictinary
            Dictionary<int, string> Name = new Dictionary<int, string>();
            Name.Add(1, "sourav");
            Name.Add(2, "Ram");
            Name.Add(3, "shyam");
 
            //Remove Value at particular Index
            Name.Remove(2);
            foreach(String name in Name.Values)
            {
                Console.WriteLine(name);
            }
   
            Console.ReadLine();
        }
    }
}

Output is like below.


Understand IDictionary interface

IDictionary is interface from where we can instantiate Dictionary class.  Have a look on below example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //Understand IDictinary
            IDictionary<string, string> dis = new Dictionary<string, string>();
            dis.Add("Sourav", "Kayal");
            dis.Add("Ram", "Chandra");
 
            Console.WriteLine("Surname of sourave is:- " +  dis["Sourav"]);
            Console.ReadLine();
        }
    }
}

Output:-


Like Dictionary, we can use all methods in IDictionary also. Here is another example.

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //Understand IDictinary
            IDictionary<int, string> student = new Dictionary<int, string>();
            student.Add(1,"Sourav");
            student.Add(2, "Ram");
            student.Add(3, "Shyam");
 
            student.Remove(2);
            Console.WriteLine("Number of Student:- " + student.Count);
            foreach (string name in student.Values)
            {
                Console.WriteLine(name);
            }
            Console.ReadLine();
        }
    }
}

Output is given below.

Conclusion:-

In this article we have learned the basic concept of Dictionary and IDictionary in C#. Hope you have understood and enjoyed it.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)