C#-এ HashSet<T> ক্লাসে নির্দিষ্ট একটি উপাদান রয়েছে কিনা তা যাচাই করতে Contains() মেথড ব্যবহার করা হয়। এটি একটি বুলিয়ান মান ফেরত দেয়। নিচে দুটি উদাহরণ দেওয়া হলো।
উদাহরণ ১
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet<int> set1 = new HashSet<int>();
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<int> set2 = new HashSet<int>();
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
উদাহরণ ২
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet<string> hashSet = new HashSet<string>();
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-এ আছে