Open In App

C# | Remove the entry with specified key from ListDictionary

Improve
Improve
Like Article
Like
Save
Share
Report

ListDictionary.Remove(Object) method is used to remove the entry with the specified key from the ListDictionary.

Syntax:

public void Remove (object key);

Here, key is the key of the entry which is to be removed.

Exception: This method will give ArgumentNullException if the key is null.

Below are the examples to illustrate the use of ListDictionary.Remove(Object) Method:

Example 1:




// C# code to remove the entry with
// the specified key from the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("Australia", "Canberra");
        myDict.Add("Belgium", "Brussels");
        myDict.Add("Netherlands", "Amsterdam");
        myDict.Add("China", "Beijing");
        myDict.Add("Russia", "Moscow");
        myDict.Add("India", "New Delhi");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                  + myDict.Count);
  
        // Displaying the key/value pairs in myDict
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }
  
        // Remove the entry with the specified
        // key from the ListDictionary
        myDict.Remove("Russia");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                 + myDict.Count);
  
        // Displaying the key/value pairs in myDict
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }
    }
}


Output:

Total key/value pairs in myDict are : 6
The key/value pairs in myDict are : 
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
Russia Moscow
India New Delhi
Total key/value pairs in myDict are : 5
The key/value pairs in myDict are : 
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
India New Delhi

Example 2 :




// C# code to remove the entry with
// the specified key from the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("I", "first");
        myDict.Add("II", "second");
        myDict.Add("III", "third");
        myDict.Add("IV", "fourth");
        myDict.Add("V", "fifth");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                  + myDict.Count);
        // Displaying the key/value pairs in myDict
  
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }
  
        // Remove the entry with the specified
        // key from the ListDictionary
        // This should raise "ArgumentNullException"
        // as the key is null
        myDict.Remove(null);
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                  + myDict.Count);
  
        // Displaying the key/value pairs in myDict
  
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }
    }
}


Runtime Error:

Unhandled Exception:
System.ArgumentNullException: Key cannot be null.
Parameter name: key

Note:

  • If the ListDictionary does not contain an element with the specified key, the ListDictionary remains unchanged. No exception is thrown.
  • This method is an O(n) operation, where n is Count.

Reference :



Last Updated : 01 Feb, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads