Removing all entries from the StringDictionary in C#

The StringDictionary class in C# provides a collection of string key-value pairs. To remove all entries from a StringDictionary, you use the Clear() method, which empties the entire collection in a single operation.

Syntax

Following is the syntax for removing all entries from a StringDictionary −

stringDictionary.Clear();

Parameters

The Clear() method takes no parameters and returns void. It removes all key-value pairs from the StringDictionary and sets the Count property to zero.

Using Clear() Method

Example

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main(){
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      Console.WriteLine("StringDictionary1 elements...");
      foreach(DictionaryEntry d in strDict1){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Does StringDictionary1 has key G? "+strDict1.ContainsKey("G"));
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("A", "John");
      strDict2.Add("B", "Andy");
      strDict2.Add("C", "Tim");
      strDict2.Add("D", "Ryan");
      strDict2.Add("E", "Kevin");
      strDict2.Add("F", "Katie");
      strDict2.Add("G", "Brad");
      Console.WriteLine("\nStringDictionary2 elements...");
      foreach(DictionaryEntry d in strDict2){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of key/value pairs in StringDictionary2 = " + strDict2.Count);
      strDict2.Clear();
      Console.WriteLine("Count of key/value pairs in StringDictionary2 (updated) = " + strDict2.Count);
   }
}

The output of the above code is −

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Does StringDictionary1 has key G? True

StringDictionary2 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Count of key/value pairs in StringDictionary2 = 7
Count of key/value pairs in StringDictionary2 (updated) = 0

Clearing and Re-populating StringDictionary

Example

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main(){
      StringDictionary strDict = new StringDictionary();
      strDict.Add("A", "John");
      strDict.Add("B", "Andy");
      strDict.Add("C", "Tim");
      strDict.Add("D", "Ryan");
      Console.WriteLine("StringDictionary elements...");
      foreach(DictionaryEntry d in strDict){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of key/value pairs in StringDictionary = " + strDict.Count);
      strDict.Clear();
      Console.WriteLine("Count of key/value pairs in StringDictionary (after Clear) = " + strDict.Count);
      
      // Re-populate after clearing
      strDict.Add("X", "Alice");
      strDict.Add("Y", "Bob");
      Console.WriteLine("StringDictionary after re-populating...");
      foreach(DictionaryEntry d in strDict){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Final count = " + strDict.Count);
   }
}

The output of the above code is −

StringDictionary elements...
a John
b Andy
c Tim
d Ryan
Count of key/value pairs in StringDictionary = 4
Count of key/value pairs in StringDictionary (after Clear) = 0
StringDictionary after re-populating...
x Alice
y Bob
Final count = 2

Key Points

  • The Clear() method removes all key-value pairs instantly.

  • After calling Clear(), the Count property becomes zero.

  • StringDictionary keys are automatically converted to lowercase.

  • You can add new entries to a StringDictionary after clearing it.

Conclusion

The Clear() method provides an efficient way to remove all entries from a StringDictionary in C#. This method is useful when you need to reset the collection completely while keeping the same StringDictionary instance for reuse.

Updated on: 2026-03-17T07:04:36+05:30

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements