Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - DICTIONARY


A Dictionary is a Collection that can also hold multiple values in Key and Value pairs. In the Dictionary, the elements are unordered, unindexed, changeable and doesn't allow duplicate values.


You can think of a Dictionary like an actual English Dictionary. Where you search for a word and you get its explanation.


The declaration of a Dictionary is also quite simple. You can place the multiple values inside braces {} in the form of Key and Value pairs.


Just imagine the Key to be the word you are going to search in the English Dictionary. And the Value is the explanation you find in it.


Creating a 'Dictionary'


Example :



using System.Collections.Generic;
    
public class MyApplication
{
    public static void Main(string[] args)
    {
        Dictionary x = new Dictionary() 
        {
	        { 5, "Is a Number" }, 
	        { "John", "Is a Name" }, 
	        { "C#", "Is a Language"}
        };
        
    
        foreach (KeyValuePair<object;, string> i in x)  
        {  
            System.Console.WriteLine("The key is : "+i.Key+" and the value is : "+i.Value);  
        }
    }    
} 


Output :



  The key is : 5 and the value is : Is a Number
  The key is : John and the value is : Is a Name
  The key is : C# and the value is : Is a Language

So, in the above code we have created a Dictionary using square braces {}. And the values are represented in Key and Value pairs.


The Key is a number or a String and in the value we have provided its explanation(Somewhat like the English Dictionary).


i.e. We know that the first element 5 is a number. So, in the Key, we have put the number 5 and in its value, we have put the explanation, Is a Number. Same logic is applicable for the other two Keys, John and C#.


A Key and Value is separated by ,.


Dictionary x = new Dictionary()
{
	{ 5, "Is a Number" },
	{ "John", "Is a Name" },
	{ "C#", "Is a Language"}
};

And initialised to the variable x.

C_Sharp

In the next line we have used the for loop to Iterate through the Dictionary.


foreach (KeyValuePair<object;, string> i in x)
{
	System.Console.WriteLine("The key is : "+i.Key+" and the value is : "+i.Value);
}

As we know, there is a Key and Value pair in a Dictionary.


foreach (KeyValuePair<object;, string> i in x)
{
	System.Console.WriteLine("The key is : "+i.Key+" and the value is : "+i.Value);
}

There are two sections in the foreach loop.


The first section is,


KeyValuePair<object;, string> i

And the second section is,


x
C_Sharp


So, the above foreach loop, the KeyValuePair<object;, string> i, creates a variable i that would take the Key and Value from Dictionary x and print them one by one.


Now, if you check the print statement inside the foreach loop,


foreach (KeyValuePair<object;, string> i in x)
{
	System.Console.WriteLine("The key is : "+i.Key+" and the value is : "+i.Value);
}

We got the Key using, i.Key and the value using i.Value.


Let us understand it in the iterations of for loop,


foreach (KeyValuePair<object;, string> i in x)

1st Iteration


In the first Iteration the first Key and Value of the Dictionary x (i.e. The key 5 and value Is a Number) is taken and put into the variables i.

C_Sharp

And the print statement,


System.Console.WriteLine("The key is : "+i.Key+" and the value is : "+i.Value);

Prints the key and value of i(i.e. The Key and the Value).

Output :



  The key is : 5 and the value is : Is a Number

2nd Iteration


Similarly, in the second Iteration the second Key and Value of the Dictionary x (i.e. The key John and Value Is a Name) is taken and put into the variables i.

C_Sharp

And the print statement, prints the value of i(i.e. The key).

Output :



  The key is : 5 and the value is : Is a Number
  The key is : John and the value is : Is a Name

3rd Iteration


Similarly, in the third Iteration the third Key and Value of the Dictionary x (i.e. The key C# and the value Is a Language) is taken and put into the variables i.

C_Sharp

And the print statement, prints the value of i(i.e. The key).

Output :



  The key is : 5 and the value is : Is a Number
  The key is : John and the value is : Is a Name
  The key is : C# and the value is : Is a Language

So, we have seen how to get the Keys. Now, let us see how can we get the corresponding Value associated with that Key.


It is just like searching a word in an English dictionary and get its corresponding value.