C# में StringDictionary से सभी एंट्रीज़ हटाने के लिए Clear() मेथड का उपयोग किया जाता है। यह मेथड डिक्शनरी को खाली कर देता है।
उदाहरण 1
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 तत्व...");
foreach (DictionaryEntry d in strDict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("क्या StringDictionary1 में कुंजी 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 तत्व...");
foreach (DictionaryEntry d in strDict2) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("StringDictionary2 में कुंजी/मान जोड़े की संख्या = " + strDict2.Count);
strDict2.Clear();
Console.WriteLine("StringDictionary2 में कुंजी/मान जोड़े की संख्या (अपडेटेड) = " + strDict2.Count);
}
}
आउटपुट
StringDictionary1 तत्व... a John b Andy c Tim d Ryan e Kevin f Katie g Brad क्या StringDictionary1 में कुंजी G है? True StringDictionary2 तत्व... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary2 में कुंजी/मान जोड़े की संख्या = 7 StringDictionary2 में कुंजी/मान जोड़े की संख्या (अपडेटेड) = 0
उदाहरण 2
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 तत्व...");
foreach (DictionaryEntry d in strDict) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("StringDictionary में कुंजी/मान जोड़े की संख्या = " + strDict.Count);
strDict.Clear();
Console.WriteLine("StringDictionary में कुंजी/मान जोड़े की संख्या (अपडेटेड) = " + strDict.Count);
}
}
आउटपुट
StringDictionary तत्व... a John b Andy c Tim d Ryan StringDictionary में कुंजी/मान जोड़े की संख्या = 4 StringDictionary में कुंजी/मान जोड़े की संख्या (अपडेटेड) = 0