Sunday, December 20, 2015

C#: Implementation of Dictionary using C#

How to implement a Dictionary using C# 


Dictionary in C# is a pretty cool data structure. In very simple words, you have a key-value pair, and these multiple key-value pairs make up the dictionary. The most important thing to note here is that you can access any value directly by using its key. The various operations performed on dictionary are:
  • The addition of pairs to the collection.
  • The removal of pairs from the collection.
  • The modification of the values of existing pairs.
  • The lookup of the value associated with a particular key.

Let us get acquainted with its definition first.

Definition

A Dictionary in C# is used to represent a collection of keys and values pair of data.

The following image shows the basic representation of a key value pair.

Image Source: scraping.pro

Key - Value Pair Example



Now if you want to access the value John, you can directly access it using the key User2.

Applications of Dictionary

  • The simplest examples that comes to mind is JSON, which is nothing but a collection of key - value pairs.
  • Secondly, Dictionaries are also used in Search Engines and Machine Learning.
 Now  let's see how this is implemented in C#.

Step 1:  Open Visual Studio Professional 2015



Step 2:  Click on New Project. Select Console Application and give desired Name to the application


Step 3:  Click on OK and you will get the following Screen




Step 4The overall implementation or the code should like the following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryConsoleExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Main Method
            // In this example we will see basic implementation of Data Structure -
            // Dictionary using the language C#.

            //Declaring a Dictionary which has keys as string and values as integer 
            //(You can make other types of Dictionaries as you want)
            Dictionary<String, int> testDictionary = new Dictionary<string, int>();

            //Inserting Key Value Pairs to the Dictionary
            Console.WriteLine("Inserting Key Value Pairs to the Dictionary");
            for (int i = 0; i < 10; i++)
            {
                testDictionary.Add("Number " + i, i);
            }
            Console.WriteLine("Adding of key value pairs to the Dictionary is finished");

            // We can directly access any element of the Dictionary using keys 
   
            Console.WriteLine("Printing the value whose key is \" Number 3\" :");

            Console.WriteLine("Value using \" Number 3 \" Key is : " + testDictionary["Number 3"]);

            // Waiting for enter to be pressed by user to prevent the Console Screen from 
            // closing automatically.
            Console.ReadLine();
        }
    }
}



Output: The output would like this:




Hope you liked the article. Let us know your feedback and questions through comments. Cheers!!

No comments:

Post a Comment

Thanks for your valuable comment