Practical usage of Removing Item from Generic Dictionary<K,T>

In this Blog we are going to see, How to Remove item from the Dictionary Collection.


Step 1:                 Adding items to Dictionary as KeyValuePair

 

            Dictionary<int, string> dic = new Dictionary<int, string>();

            dic.Add(1, "Lajapathy");

            dic.Add(2, "Parthiban");

            dic.Add(3, "AnandBabu");

            dic.Add(4, "Sathiya");

                          

Step 2:                 Displaying the Dictionary Items

 

            foreach (KeyValuePair<int, string> tempDic in dic)

            {

                Response.Write("Key :" + tempDic.Key + " value :" + tempDic.Value + "<br/>");

            }

 

Step 2:                 Removing item from the Dictionary

            bool isRemoved = dic.Remove(2); 

            Response.Write(isRemoved + "<br/>");

 

Step 3:                 Displaying Items after Removing

            foreach (KeyValuePair<int, string> tempDic in dic)

            {

                Response.Write("Key :" + tempDic.Key + " value :" + tempDic.Value + "<br/>");

            }


OUTPUT:

Key :1 value :Lajapathy
Key :2 value :Parthiban
Key :3 value :AnandBabu
Key :4 value :Sathiya


True (Tells Item is removed)
Key :1 value :Lajapathy
Key :3 value :AnandBabu
Key :4 value :Sathiya

Ebook Download
View all
Learn
View all