C#-এ Collection<T> ক্লাসে নির্দিষ্ট একটি উপাদান রয়েছে কিনা তা যাচাই করতে Contains() মেথড ব্যবহার করা হয়। এটি একটি বুলিয়ান মান ফেরত দেয়। নিচে দুটি উদাহরণ দেওয়া হলো।
উদাহরণ ১
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection<int> col = new Collection<int>();
col.Add(10);
col.Add(20);
col.Add(30);
col.Add(40);
col.Add(50);
col.Add(60);
col.Add(70);
col.Add(80);
Console.WriteLine("Collection-এ উপাদানগুলো...");
foreach (int val in col) {
Console.WriteLine(val);
}
Console.WriteLine("Collection-তে 70 উপাদান আছে? = " + col.Contains(70));
}
}
আউটপুট
Collection-এ উপাদানগুলো... 10 20 30 40 50 60 70 80 Collection-তে 70 উপাদান আছে? = True
উদাহরণ ২
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection<string> col = new Collection<string>();
col.Add("John");
col.Add("Gary");
col.Add("Tom");
col.Add("Brad");
col.Add("Katie");
col.Add("Andy");
col.Add("Nathan");
Console.WriteLine("Collection-এ উপাদানগুলো...");
foreach (string val in col) {
Console.WriteLine(val);
}
Console.WriteLine("Collection-তে 'Bradley' মান আছে? = " + col.Contains("Bradley"));
}
}
আউটপুট
Collection-এ উপাদানগুলো... John Gary Tom Brad Katie Andy Nathan Collection-তে 'Bradley' মান আছে? = False