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

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

ListDictionary থ্রেড-সেফ নয়। মাল্টি-থ্রেডেড এনভায়রনমেন্টে সেফলি ব্যবহার করার জন্য lock(dict.SyncRoot) ব্যবহার করতে হয়।

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

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 elements...");
        foreach(DictionaryEntry d in dict) {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("\nIs the ListDictionary having fixed size? = " + dict.IsFixedSize);
        Console.WriteLine("If ListDictionary read-only? = " + dict.IsReadOnly);
        Console.WriteLine("Is ListDictionary synchronized = " + dict.IsSynchronized);
        Console.WriteLine("The ListDictionary has the key K? = " + dict.Contains("K"));
        Console.WriteLine("The ListDictionary has the key 9? = " + dict.Contains("9"));

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

আউটপুট

ListDictionary elements...
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
Is the ListDictionary having fixed size? = False
If ListDictionary read-only? = False
Is ListDictionary synchronized = False
The ListDictionary has the key K? = False
The ListDictionary has the key 9? = True
Synchronize access...
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

উদাহরণ ২: সিম্পল সিঙ্ক্রোনাইজড ইটারেশন

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

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

        Console.WriteLine("Synchronize access...");
        lock(dict.SyncRoot) {
            foreach(DictionaryEntry d in dict) {
                Console.WriteLine(d.Key + " " + d.Value);
            }
        }
    }
}

আউটপুট

Synchronize access...
A Books
B Electronics
C Appliances
D Pet Supplies
E Clothing
F Footwear

সারসংক্ষেপ

  • SyncRoot প্রপার্টি লকে জন্য অবজেক্ট দেয়।
  • IsSynchronized সবসময় false রিটার্ন করে।
  • লক ব্লকের ভিতরে রিড/রাইট দুটোই করা উচিত।
  1. C#-এ ListDictionary-র নির্দিষ্ট কী-এর মান পাওয়া এবং সেট করা

  2. C#-তে Tuple-এর দ্বিতীয় উপাদান পেতে Item2 প্রোপার্টি ব্যবহার

  3. C#-তে Tuple-এর তৃতীয় উপাদান পেতে Item3 প্রোপার্টি ব্যবহার

  4. C#-তে Tuple-এর ষষ্ঠ উপাদান পেতে Item6 প্রোপার্টি ব্যবহার