C# में HashSet किसी विशिष्ट तत्व को रखता है या नहीं, यह जांचने के लिए Contains() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है।
उदाहरण 1
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet set1 = new HashSet();
set1.Add(25);
set1.Add(50);
set1.Add(75);
set1.Add(100);
set1.Add(125);
set1.Add(150);
Console.WriteLine("HashSet1 में तत्व");
foreach (int val in set1) {
Console.WriteLine(val);
}
HashSet set2 = new HashSet();
set2.Add(30);
set2.Add(60);
set2.Add(100);
set2.Add(150);
set2.Add(200);
set2.Add(250);
Console.WriteLine("HashSet2 में तत्व");
foreach (int val in set2) {
Console.WriteLine(val);
}
Console.WriteLine("क्या वे सामान्य तत्व साझा करते हैं? " + set1.Overlaps(set2));
Console.WriteLine("क्या HashSet1 में तत्व 60 है? " + set1.Contains(60));
Console.WriteLine("क्या HashSet2 में तत्व 60 है? " + set2.Contains(60));
}
}
आउटपुट
HashSet1 में तत्व 25 50 75 100 125 150 HashSet2 में तत्व 30 60 100 150 200 250 क्या वे सामान्य तत्व साझा करते हैं? True क्या HashSet1 में तत्व 60 है? False क्या HashSet2 में तत्व 60 है? True
उदाहरण 2
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet hashSet = new HashSet();
hashSet.Add("Tim");
hashSet.Add("Jack");
hashSet.Add("Matt");
hashSet.Add("Steve");
hashSet.Add("David");
hashSet.Add("Kane");
hashSet.Add("Gary");
Console.WriteLine("HashSet में तत्व");
foreach (string val in hashSet) {
Console.WriteLine(val);
}
if (hashSet.Contains("Matt"))
Console.WriteLine("तत्व Matt HashSet में है");
else
Console.WriteLine("तत्व Matt HashSet में नहीं है");
}
}
आउटपुट
HashSet में तत्व Tim Jack Matt Steve David Kane Gary तत्व Matt HashSet में है