Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Kiểm tra StringDictionary có được đồng bộ hóa trong C#

Giới thiệu

StringDictionary (namespace System.Collections.Specialized) không hỗ trợ đồng bộ hóa tích hợp. Thuộc tính IsSynchronized luôn trả về false. Không có phương thức Synchronized() static như Hashtable/ArrayList.

Ví dụ 1: Kiểm tra IsSynchronized trên hai StringDictionary

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

public class Demo
{
    public static void Main()
    {
        StringDictionary strDict1 = new StringDictionary();
        strDict1.Add("A", "John");
        strDict1.Add("B", "Andy");
        strDict1.Add("C", "Tim");
        strDict1.Add("D", "Ryan");
        strDict1.Add("E", "Kevin");
        strDict1.Add("F", "Katie");
        strDict1.Add("G", "Brad");
        
        Console.WriteLine("StringDictionary1 elements...");
        foreach (DictionaryEntry de in strDict1)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }
        
        StringDictionary strDict2 = new StringDictionary();
        strDict2.Add("1", "A");
        strDict2.Add("2", "B");
        strDict2.Add("3", "C");
        strDict2.Add("4", "D");
        strDict2.Add("5", "E");
        
        Console.WriteLine("\nStringDictionary2 key-value pairs...");
        IEnumerator demoEnum = strDict2.GetEnumerator();
        DictionaryEntry d;
        while (demoEnum.MoveNext())
        {
            d = (DictionaryEntry)demoEnum.Current;
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
        
        Console.WriteLine("Is the StringDictionary2 synchronized? = " + strDict2.IsSynchronized);
    }
}

Kết quả

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary2 key-value pairs...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E
Is the StringDictionary2 synchronized? = False

Ví dụ 2: StringDictionary khác

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

public class Demo
{
    public static void Main()
    {
        StringDictionary strDict = new StringDictionary();
        strDict.Add("One", "Laptop");
        strDict.Add("Two", "Desktop");
        strDict.Add("Three", "Earphone");
        strDict.Add("Four", "Speaker");
        strDict.Add("Five", "HardDisk");
        strDict.Add("Six", "SSD");
        strDict.Add("Seven", "HardDisk");
        
        Console.WriteLine("StringDictionary key-value pairs...");
        IEnumerator demoEnum = strDict.GetEnumerator();
        DictionaryEntry d;
        while (demoEnum.MoveNext())
        {
            d = (DictionaryEntry)demoEnum.Current;
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
        
        Console.WriteLine("Is StringDictionary synchronized? = " + strDict.IsSynchronized);
    }
}

Kết quả

StringDictionary key-value pairs...
Key = five, Value = HardDisk
Key = one, Value = Laptop
Key = four, Value = Speaker
Key = three, Value = Earphone
Key = seven, Value = HardDisk
Key = six, Value = SSD
Key = two, Value = Desktop
Is StringDictionary synchronized? = False

Đặc điểm StringDictionary

  • IsSynchronized luôn = false
  • Không có StringDictionary.Synchronized()
  • Khóa tự động chuyển thành chữ thường (case-insensitive)
  • Khóa và giá trị đều phải là string
  • Không cho phép khóa null

Làm thế nào để thread-safe?

// Cách 1: Lock thủ công
StringDictionary dict = new StringDictionary();
object lockObj = new object();

lock (lockObj) { dict.Add("key", "value"); }
lock (lockObj) { var v = dict["key"]; }

// Cách 2: Dùng ConcurrentDictionary (khuyên dùng)
var dict = new ConcurrentDictionary<string, string>();
dict.TryAdd("key", "value");
var v = dict["key"]; // Thread-safe

// Cách 3: ImmutableDictionary (read-only)
var dict = ImmutableDictionary<string, string>.Empty.Add("key", "value");

Khuyến nghị

Không dùng StringDictionary cho code mới. Thay thế bằng:

  • Dictionary<string, TValue> (generic, case-sensitive mặc định)
  • Dictionary<string, TValue>(StringComparer.OrdinalIgnoreCase) (case-insensitive)
  • ConcurrentDictionary<string, TValue> (thread-safe)