C# में Collection<T> किसी विशिष्ट तत्व को रखता है या नहीं, यह जांचने के लिए Contains() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है।
उदाहरण 1
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection col = new Collection();
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
उदाहरण 2
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection col = new Collection();
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