StringDictionary.Remove(String) method is used to remove the entry with the specified key from the string dictionary.
Syntax:
public virtual void Remove (string key);
Here, key is the key of the entry to remove.
Exceptions:
- ArgumentNullException: If the key is null.
- NotSupportedException: If the StringDictionary is read-only.
Below programs illustrate the use of StringDictionary.Remove(String) method:
Example 1:
// C# code to remove the entry // with the specified key from // the StringDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("A", "Apple"); myDict.Add("B", "Banana"); myDict.Add("C", "Cat"); myDict.Add("D", "Dog"); myDict.Add("E", "Elephant"); myDict.Add("F", "Fish"); // Displaying the keys and values in StringDictionary Console.WriteLine("The number of key/value pairs are : " + myDict.Count); foreach(DictionaryEntry dic in myDict) { Console.WriteLine(dic.Key + " " + dic.Value); } // Removing the entry with the specified // key from the StringDictionary myDict.Remove("D"); // Displaying the keys and values in StringDictionary Console.WriteLine("The number of key/value pairs are : " + myDict.Count); foreach(DictionaryEntry dic in myDict) { Console.WriteLine(dic.Key + " " + dic.Value); } } } |
Output:
The number of key/value pairs are : 6 b Banana c Cat a Apple f Fish d Dog e Elephant The number of key/value pairs are : 5 b Banana c Cat a Apple f Fish e Elephant
Example 2:
// C# code to remove the entry // with the specified key from // the StringDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a StringDictionary named myDict StringDictionary myDict = new StringDictionary(); // Adding key and value into the StringDictionary myDict.Add("A", "Apple"); myDict.Add("B", "Banana"); myDict.Add("C", "Cat"); myDict.Add("D", "Dog"); myDict.Add("E", "Elephant"); myDict.Add("F", "Fish"); // Displaying the keys and values in StringDictionary Console.WriteLine("The number of key/value pairs are : " + myDict.Count); foreach(DictionaryEntry dic in myDict) { Console.WriteLine(dic.Key + " " + dic.Value); } // Removing the entry with the specified // key from the StringDictionary // This should raise "ArgumentNullException" // as the key is null myDict.Remove(null); // Displaying the keys and values in StringDictionary Console.WriteLine("The number of key/value pairs are : " + myDict.Count); foreach(DictionaryEntry dic in myDict) { Console.WriteLine(dic.Key + " " + dic.Value); } } } |
Runtime Error:
Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: key
Note:
- If the StringDictionary does not contain an element with the specified key, the StringDictionary remains unchanged. No exception is thrown.
- The key is handled in a case-insensitive manner, i.e, it is translated to lowercase before it is used to find the entry to remove from the StringDictionary.
- This method is an O(1) operation.
Reference:
Recommended Posts:
- C# | Remove the entry with specified key from ListDictionary
- C# | Remove entry with specified key from OrderedDictionary
- C# | Get or set the value associated with the specified key in StringDictionary
- C# | Removing the specified key entry from HybridDictionary
- C# | Insert a new entry in OrderedDictionary with specified key and value
- C# | Gets or sets the value at the specified key in StringDictionary
- C# | Remove the entry at specified index from OrderedDictionary
- C# | Copy StringDictionary to Array at the specified index
- C# | Get the number of key/value pairs in the StringDictionary
- C# | Check if the StringDictionary contains a specific key
- C# | Add key and value into StringDictionary
- C# | Remove the element with the specified key from the Hashtable
- C# | Remove the element with the specified key from a SortedList
- C# | Check if the StringDictionary contains a specific value
- C# | Removing all entries from the StringDictionary
- C# | Check if StringDictionary is synchronized (thread safe)
- C# | Get an enumerator that iterates through the stringDictionary
- C# | Get a collection of keys in the StringDictionary
- C# | Get a collection of values in the StringDictionary
- C# | StringDictionary Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

