কম্পিউটার টিউটোরিয়াল

C#-এ StringDictionary-এ কিগুলোর কালেকশন পাওয়া (Keys প্রপার্টি)


C#-এ StringDictionary.Keys প্রপার্টি একটি ICollection রিটার্ন করে যাতে StringDictionary-এ থাকা সব কি (keys) থাকে। StringDictionary একটি কেস-ইনসেনসিটিভ (case-insensitive) স্ট্রিং-টু-স্ট্রিং ডিকশনারি।

উদাহরণ ১: দুটি StringDictionary এবং কি কালেকশন

using System; 
using System.Collections; 
using System.Collections.Specialized; 
public class Demo { 
    public static void Main(){ 
        StringDictionary strDict1 = new StringDictionary(); 
        strDict1.Add("U", "Electronics"); 
        strDict1.Add("V", "Toys"); 
        strDict1.Add("W", "Books"); 
        strDict1.Add("X", "Accessories"); 
        Console.WriteLine("StringDictionary1 elements..."); 
        foreach(DictionaryEntry d in strDict1){ 
            Console.WriteLine(d.Key + " " + d.Value); 
        } 
        Console.WriteLine("Does StringDictionary1 has key G? " + strDict1.ContainsKey("G")); 
        StringDictionary strDict2 = new StringDictionary(); 
        strDict2.Add("A", "John"); 
        strDict2.Add("B", "Andy"); 
        strDict2.Add("C", "Tim"); 
        strDict2.Add("D", "Ryan"); 
        strDict2.Add("E", "Kevin"); 
        strDict2.Add("F", "Katie"); 
        strDict2.Add("G", "Brad"); 
        Console.WriteLine("\nStringDictionary2 elements..."); 
        foreach(DictionaryEntry d in strDict2){ 
            Console.WriteLine(d.Key + " " + d.Value); 
        } 
        Console.WriteLine("\nCollection of keys (StringDictionary2)..."); 
        String[] keyArr = new String[strDict2.Count]; 
        strDict2.Keys.CopyTo(keyArr, 0); 
        for (int i = 0; i < strDict2.Count; i++) { 
            Console.WriteLine("Key " + (i+1) + " = " + keyArr[i]); 
        } 
        Console.WriteLine("\nIs Dictionary2 equal to Dictionary1? = " + strDict2.Equals(strDict1)); 
        Console.WriteLine("Does StringDictionary2 has key B? " + strDict2.ContainsKey("B")); 
    } 
}

আউটপুট

StringDictionary1 elements... 
x Accessories 
u Electronics 
v Toys 
w Books 
Does StringDictionary1 has key G? False 
StringDictionary2 elements... 
a John 
b Andy 
c Tim 
d Ryan 
e Kevin 
f Katie 
g Brad 
Collection of keys (StringDictionary2)... 
Key 1 = a 
Key 2 = b 
Key 3 = c 
Key 4 = d 
Key 5 = e 
Key 6 = f 
Key 7 = g 
Is Dictionary2 equal to Dictionary1? = False 
Does StringDictionary2 has key B? True

উদাহরণ ২: কি এবং মান একসাথে প্রদর্শন

using System; 
using System.Collections; 
using System.Collections.Specialized; 
public class Demo { 
    public static void Main(){ 
        StringDictionary strDict1 = new StringDictionary(); 
        strDict1.Add("S", "Home Appliances"); 
        strDict1.Add("T", "Smart Wearable Tech"); 
        strDict1.Add("U", "Electronics"); 
        strDict1.Add("V", "Toys"); 
        strDict1.Add("W", "Books"); 
        strDict1.Add("X", "Accessories"); 
        Console.WriteLine("StringDictionary elements..."); 
        foreach(DictionaryEntry d in strDict1){ 
            Console.WriteLine(d.Key + " " + d.Value); 
        } 
        Console.WriteLine("\nCollection of keys and values..."); 
        String[] keyArr = new String[strDict1.Count]; 
        strDict1.Keys.CopyTo(keyArr, 0); 
        for (int i = 0; i < strDict1.Count; i++) { 
            Console.WriteLine("Key " + (i+1) + " = " + keyArr[i] + ", Value " + (i+1) + " = " + strDict1[keyArr[i]]); 
        } 
        Console.WriteLine("\nDoes StringDictionary has key B? " + strDict1.ContainsKey("B")); 
    } 
}

আউটপুট

StringDictionary elements... 
x Accessories 
s Home Appliances 
t Smart Wearable Tech 
u Electronics 
v Toys 
w Books 
Collection of keys and values... 
Key 1 = x, Value 1 = Accessories 
Key 2 = s, Value 2 = Home Appliances 
Key 3 = t, Value 3 = Smart Wearable Tech 
Key 4 = u, Value 4 = Electronics 
Key 5 = v, Value 5 = Toys 
Key 6 = w, Value 6 = Books 
Does StringDictionary has key B? False

StringDictionary-এর বৈশিষ্ট্য

  • কেস-ইনসেনসিটিভ কি: "Key" এবং "key" একই বিবেচিত হয়।
  • স্ট্রিং-টু-স্ট্রিং: কি এবং মান উভয়ই স্ট্রিং হতে হবে।
  • নাল মান অনুমোদিত: মান হিসেবে null দেওয়া যায়।
  • অর্ডার নিশ্চিত নয়: এনুমেরেশন অর্ডার অনির্দিষ্ট।
  • লিগ্যাসি ক্লাস: নতুন কোডে Dictionary<string, string> (StringComparer.OrdinalIgnoreCase সহ) ব্যবহার করা ভালো।

মূল পয়েন্টস

  • Keys প্রপার্টি সব কির একটি কালেকশন রিটার্ন করে।
  • CopyTo() মেথড ব্যবহার করে অ্যারে-এ কপি করা যায়।
  • ContainsKey() মেথড কেস-ইনসেনসিটিভভাবে কি খুঁজে বের করে।
  • Equals() রেফারেন্স সমতা চেক করে, কন্টেন্ট সমতা নয়।
  1. উইন্ডোজে ক্যাপস লক কী চাপলে সতর্কতা পান

  2. C#-এ Dictionary থেকে কীগুলোর লিস্ট পাওয়া

  3. MongoDB কালেকশন থেকে সব ফিল্ড নাম দক্ষতার সাথে রিট্রিভ করুন

  4. MongoDB কালেকশনে সকল কি নাম সহজে রিট্রিভ করুন