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

C#-এ StringDictionary-এ সিঙ্ক্রোনাইজড অ্যাক্সেস পাওয়া

StringDictionary থ্রেড-সেফ নয়। মাল্টি-থ্রেডেড কোডে সেফলি অ্যাক্সেসের জন্য lock(strDict.SyncRoot) ব্যবহার করতে হয়।

উদাহরণ ১: StringDictionary সিঙ্ক্রোনাইজ করা

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringDictionary strDict = new StringDictionary();
        strDict.Add("A", "Books");
        strDict.Add("B", "Electronics");
        strDict.Add("C", "Appliances");
        strDict.Add("D", "Pet Supplies");
        strDict.Add("E", "Clothing");
        strDict.Add("F", "Footwear");

        Console.WriteLine("StringDictionary key-value pairs...");
        foreach(DictionaryEntry de in strDict) {
            Console.WriteLine(de.Key + " " + de.Value);
        }

        Console.WriteLine("Value associated with key D = " + strDict["D"]);
        Console.WriteLine("Value associated with key F = " + strDict["F"]);

        Console.WriteLine("\nSynchronize access..");
        lock(strDict.SyncRoot) {
            foreach(DictionaryEntry de in strDict) {
                Console.WriteLine(de.Key + " " + de.Value);
            }
        }
    }
}

আউটপুট

StringDictionary key-value pairs...
a Books
b Electronics
c Appliances
d Pet Supplies
e Clothing
f Footwear
Value associated with key D = Pet Supplies
Value associated with key F = Footwear
Synchronize access..
a Books
b Electronics
c Appliances
d Pet Supplies
e Clothing
f Footwear

উদাহরণ ২: অন্য উদাহরণ

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringDictionary strDict = new StringDictionary();
        strDict.Add("A", "John");
        strDict.Add("B", "Andy");
        strDict.Add("C", "Tim");
        strDict.Add("D", "Ryan");
        strDict.Add("E", "Kevin");
        strDict.Add("F", "Katie");
        strDict.Add("G", "Brad");

        Console.WriteLine("StringDictionary elements...");
        foreach(DictionaryEntry de in strDict) {
            Console.WriteLine(de.Key + " " + de.Value);
        }

        Console.WriteLine("\nSynchronize access..");
        lock(strDict.SyncRoot) {
            foreach(DictionaryEntry de in strDict) {
                Console.WriteLine(de.Key + " " + de.Value);
            }
        }
    }
}

আউটপুট

StringDictionary elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Synchronize access..
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad

নোট

  • StringDictionary কীগুলো আन्तরিকভাবে ছোট হাতের অক্ষরে (lowercase) কনভার্ট করে স্টোর করে।
  • SyncRoot লকে জন্য অবজেক্ট প্রদান করে।
  • আধুনিক কোডে Dictionary<string,string> পছন্দনೀಯ (জেনেরিক, ফাস্ট)।
  1. C#-তে OrderedDictionary-র শুধুমাত্র পঠনযোগ্য (Read-Only) কপি পেতে AsReadOnly() মেথড ব্যবহার

  2. C#-এ StringDictionary-র মান সংগ্রহ পান

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

  4. উইন্ডোজ ১০ ও ৮ কুইক অ্যাক্সেস মেনু (Win+X) কাস্টোমাইজ করুন: ধাপে ধাপে গাইড