उदाहरण
HashSet<T> की ExceptWith() मेथड वर्तमान सेट से उन सभी तत्वों को हटा देती है जो निर्दिष्ट कलेक्शन में मौजूद हैं। यह सेट को संशोधित करता है ताकि इसमें केवल वही तत्व रहें जो निर्दिष्ट कलेक्शन में नहीं हैं।
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
HashSet<string> set1 = new HashSet<string>();
set1.Add("Ryan");
set1.Add("Tom");
set1.Add("Andy");
set1.Add("Tim");
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));
// set2 से उन तत्वों को हटाएं जो set1 में हैं
set2.ExceptWith(set1);
// set2 में शेष तत्व प्रदर्शित करें (जो set1 में नहीं हैं)
foreach(string i in set2) {
Console.WriteLine(i);
}
}
}
आउटपुट
HashSet1 में तत्व... Ryan Tom Andy Tim HashSet2 में तत्व... John Jacob Ryan Tom Andy Tim Steve Mark क्या HashSet1, HashSet2 के बराबर है? = False John Jacob Steve Mark
एक और उदाहरण
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
HashSet<string> set1 = new HashSet<string>();
set1.Add("Jacob");
set1.Add("Ryan");
set1.Add("Tom");
set1.Add("Andy");
set1.Add("Tim");
set1.Add("Steve");
set1.Add("Mark");
Console.WriteLine("HashSet1 में तत्व...");
foreach (string res in set1) {
Console.WriteLine(res);
}
HashSet<string> set2 = new HashSet<string>();
set2.Add("Kevin");
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));
set2.ExceptWith(set1);
foreach(string i in set2) {
Console.WriteLine(i);
}
}
}
आउटपुट
HashSet1 में तत्व... Jacob Ryan Tom Andy Tim Steve Mark HashSet2 में तत्व... Kevin Jacob Ryan Tom Andy Tim Steve Mark क्या HashSet1, HashSet2 के बराबर है? = False Kevin