C#-এ ListDictionary ক্লাসের Contains মেথড ব্যবহার করে আপনি সহজেই যাচাই করতে পারেন যে কোনো নির্দিষ্ট কী ডিকশনারিতে আছে কিনা। এই মেথড একটি বুলিয়ান মান রিটার্ন করে: কী থাকলে true, নাইলে false।
উদাহরণ ১: কী যাচাই করা
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 উপাদানসমূহ...");
foreach(DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nListDictionary2 কী-মান জোড়া...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("ListDictionary2-র নির্দিষ্ট আকার? = " + dict2.IsFixedSize);
Console.WriteLine("ListDictionary2 রিড-অনলি? = " + dict2.IsReadOnly);
Console.WriteLine("ListDictionary2 সিঙ্ক্রোনাইজড? = " + dict2.IsSynchronized);
Console.WriteLine("ListDictionary2-তে '5' কী আছে? = " + dict2.Contains("5"));
}
}
আউটপুট
ListDictionary1 উপাদানসমূহ...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
ListDictionary2 কী-মান জোড়া...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
ListDictionary2-র নির্দিষ্ট আকার? = False
ListDictionary2 রিড-অনলি? = False
ListDictionary2 সিঙ্ক্রোনাইজড? = False
ListDictionary2-তে '5' কী আছে? = True
উদাহরণ ২: আরও একটি উদাহরণ
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add("1", "SUV");
dict.Add("2", "Sedan");
dict.Add("3", "Utility Vehicle");
dict.Add("4", "Compact Car");
dict.Add("5", "SUV");
dict.Add("6", "Sedan");
dict.Add("7", "Utility Vehicle");
dict.Add("8", "Compact Car");
dict.Add("9", "Crossover");
dict.Add("10", "Electric Car");
Console.WriteLine("ListDictionary উপাদানসমূহ...");
foreach(DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nListDictionary-র নির্দিষ্ট আকার? = " + dict.IsFixedSize);
Console.WriteLine("ListDictionary রিড-অনলি? = " + dict.IsReadOnly);
Console.WriteLine("ListDictionary সিঙ্ক্রোনাইজড? = " + dict.IsSynchronized);
Console.WriteLine("ListDictionary-তে 'M' কী আছে? = " + dict.Contains("M"));
}
}
আউটপুট
ListDictionary উপাদানসমূহ...
1 SUV
2 Sedan
3 Utility Vehicle
4 Compact Car
5 SUV
6 Sedan
7 Utility Vehicle
8 Compact Car
9 Crossover
10 Electric Car
ListDictionary-র নির্দিষ্ট আকার? = False
ListDictionary রিড-অনলি? = False
ListDictionary সিঙ্ক্রোনাইজড? = False
ListDictionary-তে 'M' কী আছে? = False
সারাংশ: Contains(key) মেথড দিয়ে O(n) সময়ে কী খুঁজে বের করা যায়। বড় ডেটার জন্য Hashtable বা Dictionary<TKey, TValue> ব্যবহার করা ভালো।