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




C# - REMOVE FROM DICTIONARY


How to remove an element from the Dictionary?


Let us say, we have a Dictionary that contains,

5, Is a Number
John, Is a Name
C#, Is a Language

As Key and Value pair.


Now, if you want to remove the entries from a Dictionary, Remove() and Clear() Method can be used.


Let us look at the Remove() Method first.


How to remove an element using the Remove() Method?


The Remove() Method can be used to remove an item from the Dictionary.


Let us say, we want to remove the entry where the Key is 5 and Value is Is a Number.

5, Is a Number

Example :



using System.Collections.Generic;
    
public class MyApplication
{
    public static void Main(string[] args)
    {
        Dictionary<object, string> x = new Dictionary<object, string>() 
        {
	        { 5, "Is a Number" }, 
	        { "John", "Is a Name" }, 
	        { "C#", "Is a Language"}
        };
        
        x.Remove(5);
    
        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 : 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 braces {} and Key and Value pairs.


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

And initialised to the variable x.

C_Sharp

Now, we are supposed to delete the value of the Key, 5.


So, we have used the below way to update it.


x.Remove(5);

And the entry for the Key 5 is deleted for the Dictionary.

C_Sharp

And we get the below output,

Output :



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

How to remove all the elements from the Dictionary using Clear() Method?


The Clear() Method can be used to remove all the elements from the Dictionary.


Example :



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


Output :




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


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

And initialised to the variable x.

C_Sharp

And we have used the Clear() method to remove all the elements from the Dictionary.


x.Clear()

And the print statement, prints an empty Dictionary.