C# | Remove all entries from the ListDictionary
ListDictionary.Clear method is used to remove the all entries from the ListDictionary.
Syntax:
public void Clear ();
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to remove all entries // 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); } // Removing all entries from the ListDictionary myDict.Clear(); // 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); } } } |
chevron_right
filter_none
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 : 0 The key/value pairs in myDict are :
Example 2:
// C# code to remove all entries // 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); } // Removing all entries from the ListDictionary myDict.Clear(); // 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); } } } |
chevron_right
filter_none
Output:
Total key/value pairs in myDict are : 5 The key/value pairs in myDict are : I first II second III third IV fourth V fifth Total key/value pairs in myDict are : 0 The key/value pairs in myDict are :
Note:
- Count is set to zero, and references to other objects from elements of the collection are also released.
- This method is an O(1) operation.
Reference:
Recommended Posts:
- C# | Remove the entry with specified key from ListDictionary
- C# | Add the specified key and value into the ListDictionary
- C# | Get or set the value associated with specified key in ListDictionary
- How to create a ListDictionary in C#
- C# | ListDictionary Class
- C# | Check if ListDictionary contains a specific key
- C# | Check if ListDictionary is read-only
- C# | Get an ICollection containing the keys in ListDictionary
- C# | Get an enumerator that iterates through the ListDictionary
- How to get Synchronize access to the ListDictionary in C#
- C# | Get an ICollection containing the values in ListDictionary
- C# | Check if ListDictionary has a fixed size
- C# | Check if two ListDictionary objects are equal
- C# | Get the number of key/value pairs contained in ListDictionary
- C# | Copy ListDictionary to Array instance at the specified index
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.



