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:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringDictionary myDict = new 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" );
Console.WriteLine( "The number of key/value pairs are : " + myDict.Count);
foreach (DictionaryEntry dic in myDict)
{
Console.WriteLine(dic.Key + " " + dic.Value);
}
myDict.Clear();
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 : 0
Note: This method is an O(n) operation, where n is Count.
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Feb, 2019
Like Article
Save Article