उदाहरण
HashSet<T> की Clear() मेथड सेट से सभी तत्वों को हटा देती है। इस उदाहरण में दो HashSet बनाए गए हैं, दूसरे में तत्व जोड़े गए हैं, फिर Clear() कॉल करके खाली किया गया है।
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
HashSet<string> set1 = new HashSet<string>();
set1.Add("A");
set1.Add("B");
set1.Add("C");
set1.Add("D");
set1.Add("E");
set1.Add("F");
set1.Add("G");
set1.Add("H");
Console.WriteLine("HashSet1 में तत्व...");
foreach (string res in set1) {
Console.WriteLine(res);
}
HashSet<string> set2 = new HashSet<string>();
set2.Add("John");
set2.Add("Jacob");
set2.Add("Ryan");
set2.Add("Tom");
set2.Add("Andy");
set2.Add("Tim");
set2.Add("Steve");
set2.Add("Mark");
Console.WriteLine("HashSet2 में तत्व...");
foreach (string res in set2) {
Console.WriteLine(res);
}
Console.WriteLine("क्या HashSet1, HashSet2 के बराबर है? = " + set1.Equals(set2));
Console.WriteLine("HashSet2 की संख्या = " + set2.Count);
set2.Clear();
Console.WriteLine("HashSet2 की संख्या (अपडेटेड) = " + set2.Count);
}
}
आउटपुट
HashSet1 में तत्व... A B C D E F G H HashSet2 में तत्व... John Jacob Ryan Tom Andy Tim Steve Mark क्या HashSet1, HashSet2 के बराबर है? = False HashSet2 की संख्या = 8 HashSet2 की संख्या (अपडेटेड) = 0
एक और उदाहरण
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
HashSet<string> set1 = new HashSet<string>();
set1.Add("A");
set1.Add("B");
set1.Add("C");
set1.Add("D");
set1.Add("E");
set1.Add("F");
set1.Add("G");
set1.Add("H");
Console.WriteLine("HashSet में तत्व...");
foreach (string res in set1) {
Console.WriteLine(res);
}
Console.WriteLine("HashSet1 की संख्या = " + set1.Count);
set1.Clear();
Console.WriteLine("HashSet1 की संख्या (अपडेटेड) = " + set1.Count);
}
}
आउटपुट
HashSet में तत्व... A B C D E F G H HashSet1 की संख्या = 8 HashSet1 की संख्या (अपडेटेड) = 0