C# | Removing all entries from the StringDictionary
StringDictionary.Clear method is used to remove all the entries from the StringDictionary.
Syntax:
public virtual void Clear ();
Exception: This method will give the NotSupportedException if the StringDictionary is read-only.
Example:
// C# code to remove all entries // 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 all entries from the StringDictionary myDict.Clear(); // 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); } } } |
chevron_right
filter_none
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 : 0
Note: This method is an O(n) operation, where n is Count.
Reference:
Recommended Posts:
- C# | Removing all entries from HybridDictionary
- C# | Add key and value into StringDictionary
- C# | Get or set the value associated with the specified key in StringDictionary
- How to create a StringDictionary in C#
- C# | Gets or sets the value at the specified key in StringDictionary
- C# | StringDictionary Class
- C# | Get the number of key/value pairs in the StringDictionary
- C# | Remove entry with specified key from the StringDictionary
- C# | Check if the StringDictionary contains a specific value
- How to get Synchronize access to the StringDictionary in C#
- C# | Check if the StringDictionary contains a specific key
- 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# | Remove all entries from the ListDictionary
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.


