Introduction to Dictionary Collection

Introduction

In this article you learn about the Dictionary collection, including how to use it and what the operations are that can be done on a Dictionary collection.

Dictionary

It is a collection of key and value pairs of data. It uses keys and values of a specified type including int and string. The key in the Dictionary should not be null but the value can be null. This class is defined in the following namespace that you can just import to use a Dictionary:

  1. using System.Collections.Generic;  
Syntax

Dictionary<TKey, TValue>

Parameters

TKey: The type of key used in the Dictionary.
TValue: The type of value used in the Dictionary.

Adding Values

The Add method is used to add values to the Dictionary, it takes two parameters; one for the key and the other for the value.

Syntax
  1. var dic = new Dictionary<intstring>();  
  2. dic.Add(1,"Krishna");
Here, in the above code dic is an object of Dictionary. The key type is int and the value type is string. It defines the krishna value added to the Dictionary.

Output

add record in dictionary


Removing Values

You can remove any key/value pairs from the Dictionary. Just pass the key value to the remove method.

Syntax
  1. var dic = new Dictionary<stringstring>();  
  2. dic.Add("K","Krishna");  
  3. dic.Add("R""Radha");  
  4. dic.Add("J""Jeetendra");  
  5. dic.Add("S""Sai");  
  6. dic.Remove("S");  
Here, we add 4 values to the Dictionary and finally remove the key value "S" from the Dictionary.

Output

remove record in dictionary


Count

This method is used to determine the total number of key/value pairs present in the Dictionary.

Syntax
  1. dic.Count();  
Here, dic is the Dictionary parameter.

Output

record in dictionary


Clear

If you want to remove all the pairs from the Dictionary we used this method.

Syntax

  1. dic.Clear();  
Retrive key/value pair from the Dictionary using a foreach loop
  1. foreach (KeyValuePair<stringstring> item in dic)  
  2. {  
  3.       Console.WriteLine("Key ->" + item.Key.ToString() + "Value ->" + item.Value.ToString());  
  4. }  
Here, KeyValuePair stores two values together, it is simple and available in the Dictionary. You can use var also replacing KeyValuePair.

Output

retrive values in dictionary


ContainsValue and ContainsKey

In the Dictionary class these two methods are used to search for a key or value in the Dictionary in a simple way.
  1. if (dic.ContainsKey("K"))  
  2. {  
  3.       Console.WriteLine("Key is Present");  
  4. }  
  5.       if (dic.ContainsValue("Jeetendra"))  
  6. {  
  7.       Console.WriteLine("Value is Present");  
  8. }  
This is a simple example to search for a key or value (if present) in a Dictionary.

Output

key status

Demo Example

In this example you learn in detail about Dictionary.
  1. Program.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4.   
  5. namespace Dictionary_Demo  
  6. {  
  7.       class Program  
  8.       {  
  9.             static void Main(string[] args)  
  10.             {  
  11.                   //Creating Dictionary  
  12.                   var dic = new Dictionary<stringstring>();  
  13.   
  14.                   //Adding values to the Dictionary  
  15.                   dic.Add("K","Krishna");  
  16.                   dic.Add("R""Radha");  
  17.                   dic.Add("J""Jeetendra");  
  18.                   dic.Add("S""Sai");  
  19.   
  20.                   Console.WriteLine("After Adding the record in Dictionary");  
  21.                   //Retrive values using simple way  
  22.                   foreach (var pair in dic)  
  23.                   {  
  24.                         Console.WriteLine("Key ->" + pair.Key);  
  25.                         Console.WriteLine("Value ->" + pair.Value);  
  26.                   }  
  27.   
  28.                   //Total number of pairs present in Dictionary  
  29.                   var cnt = dic.Count;  
  30.                   Console.WriteLine("Total Number of Pairs is " + cnt);  
  31.   
  32.                   //Removing values from Dictionary  
  33.                   dic.Remove("S");  
  34.   
  35.                   Console.WriteLine("After Removing key in Dictionary");  
  36.                   foreach (var pair in dic)  
  37.                   {  
  38.                         Console.WriteLine("Key ->" + pair.Key);  
  39.                         Console.WriteLine("Value ->" + pair.Value);  
  40.                   }  
  41.   
  42.                   Console.WriteLine("");  
  43.   
  44.                   //Retrive values using KeyValuePair  
  45.                   Console.WriteLine("Retrive values using KeyValuePair");  
  46.                   foreach (KeyValuePair<stringstring> item in dic)  
  47.                   {  
  48.                         Console.WriteLine("Key ->" + item.Key.ToString() + "Value ->" + item.Value.ToString());  
  49.                   }  
  50.   
  51.                   Console.WriteLine("");  
  52.   
  53.                   //Searching Key in Dictionary  
  54.                   if (dic.ContainsKey("K"))  
  55.                   {  
  56.                          Console.WriteLine("Key is Present");  
  57.                   }  
  58.                   else  
  59.                   {  
  60.                         Console.WriteLine("Key is not Present");  
  61.                   }  
  62.   
  63.                   Console.WriteLine("");  
  64.   
  65.                   //Searching Value in Dictionary  
  66.                   if (dic.ContainsValue("Jeetendra"))  
  67.                   {  
  68.                         Console.WriteLine("Value is Present");  
  69.                   }  
  70.                   else  
  71.                   {  
  72.                         Console.WriteLine("Value is not Present");  
  73.                   }  
  74.   
  75.                   //Remove all pairs from Dictionary  
  76.                   dic.Clear();  
  77.   
  78.                   Console.WriteLine("Finish");  
  79.                   Console.ReadKey();   
  80.            }  
  81.       }  
  82. }  
Note: For detailed code please download the Zip file attached above.

Summary

I hope you now understand the creation and use of a Dictionary. If you have any suggestion regarding this article then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all