Various Operations in Dictionary Using C#

Introduction

Dictionary<> is an implemented class in the System.Collections.Generic namespace. A Dictionary is a lookup table that allows objects as keys and objects as values. A Dictionary is used when we have many different 
elements. We must specify the types of its keys and values.

ADD Keys in Dictionary

The following example shows how to add the values in the dictionary and how to display the content of the dictionary.

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creation od dictionary
            Dictionary<string, int> dictionary =new Dictionary<string, int>();
            //add keys and values in the dictionary
            dictionary.Add("MCA", 1);
            dictionary.Add("BCA", 2);
            dictionary.Add("BTech", 3);
            dictionary.Add("MTech", 4);
            //display the dictionary using foreach loop
            foreach (KeyValuePair<string, int> pair in dictionary)
            {
                Console.WriteLine("{0}, {1}",pair.Key,pair.Value);
            }
        }
    }
}

Output

dictionary1.jpg

ContainsKey

Determine whether the key is present in the dictionary or not using the ContainsKey method. This method is used to find the key. The following code for finding the key in the dictionary is:

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creation od dictionary
            Dictionary<string, int> dictionary =new Dictionary<string, int>();
            //add keys and values in the dictionary
            dictionary.Add("MCA", 1);
            dictionary.Add("BCA", 2);
            dictionary.Add("BTech", 3);
            dictionary.Add("MTech", 4);
            //display the dictionary using foreach loop
            foreach (KeyValuePair<string, int> pair in dictionary)
            {
                Console.WriteLine("{0}, {1}",pair.Key,pair.Value);
            }
            if (dictionary.ContainsKey("MCA"))
            {
                int value = dictionary["MCA"];
                Console.WriteLine("The value is:"+value);
            }
            // See whether Dictionary contains this string.
            if (!dictionary.ContainsKey("MBA"))
            {
                Console.WriteLine(false);
            }
        }
    }
}

Output

dictionary2.jpg

ContainsValue

Dictionary also helpfully implements a method called ContainsValue. This method is used to find the value.

It searches the entire collection. The following code is used to implement the ContainValue:

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creation od dictionary
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            //add keys and values in the dictionary
            dictionary.Add("MCA", 1);
            dictionary.Add("BCA", 2);
            dictionary.Add("BTech", 3);
            dictionary.Add("MTech", 4);
            //use of containvalue method
            if (dictionary.ContainsValue(1))
            {
                Console.WriteLine(true);
            }
            //display the dictionary using foreach loop
            foreach (KeyValuePair<string, int> pair in dictionary)
            {
                Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
            }
        }
    }
}

Output

dictionary3.jpg

Remove

The following code is used to remove values from the dictionary:

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creation od dictionary
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            //add keys and values in the dictionary
            dictionary.Add("MCA", 1);
            dictionary.Add("BCA", 2);
            dictionary.Add("BTech", 3);
            dictionary.Add("MTech", 4);
            //display the dictionary using foreach loop
            foreach (KeyValuePair<string, int> pair in dictionary)
            {
                Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
            }
            //REMOVE the element
            dictionary.Remove("BTech");
            dictionary.Remove("BCA");
            Console.WriteLine("after removal the dictionary items are:");
            foreach (KeyValuePair<string, int> pair in dictionary)
            {
                Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
            }
        }
    }
}

Output

dictionary4.jpg

Summary

In this article, I explained how to create a dictionary, add and remove elements from the dictionary and how to find the element from the dictionary using the ContainsKey and ContainsValue methods.

Read more: Dictionary In C#


Similar Articles