Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

คลาส HybridDictionary ใน C#

ภาพรวม

HybridDictionary นำ IDictionary ไปปฏิบัติโดยใช้ ListDictionary เมื่อข้อมูลน้อย และสลับเป็น Hashtable เมื่อข้อมูลเยอะขึ้น อัตโนมัติ เหมาะสำหรับกรณีที่ไม่แน่ใจขนาดคอลเลกชัน

คุณสมบัติหลัก

ลำดับคุณสมบัติคำอธิบาย
1Countจำนวนคู่คีย์/ค่า
2IsFixedSizeขนาดคงที่หรือไม่
3IsReadOnlyอ่านอย่างเดียวหรือไม่
4IsSynchronizedthread-safe หรือไม่
5Item[Object]เข้าถึงค่าผ่านคีย์
6Keysคอลเลกชันคีย์
7SyncRootออบเจ็กต์สำหรับซิงโครไนซ์
8Valuesคอลเลกชันค่า

เมธอดหลัก

ลำดับเมธอดคำอธิบาย
1Add(Object, Object)เพิ่มรายการ
2Clear()ลบทั้งหมด
3Contains(Object)ตรวจสอบคีย์
4CopyTo(Array, Int32)คัดลอกไปอาร์เรย์
5GetEnumerator()คืน IDictionaryEnumerator

ตัวอย่างที่ 1: นับจำนวนคู่คีย์/ค่า และ Clear

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

public class Demo
{
    public static void Main()
    {
        HybridDictionary dict1 = new HybridDictionary();
        dict1.Add("A", "SUV");
        dict1.Add("B", "MUV");
        dict1.Add("C", "AUV");

        Console.WriteLine("HybridDictionary1 elements...");
        foreach (DictionaryEntry d in dict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Count of Key/value pairs in Dictionary1 = " + dict1.Count);

        HybridDictionary dict2 = new HybridDictionary();
        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("\nHybridDictionary2 elements...");
        foreach (DictionaryEntry d in dict2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Count of Key/value pairs in Dictionary2 = " + dict1.Count); // บัคในโค้ดต้นฉบับ: ควรเป็น dict2.Count
        dict2.Clear();
        Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = " + dict2.Count);
    }
}

ผลลัพธ์

HybridDictionary1 elements...
A SUV
B MUV
C AUV
Count of Key/value pairs in Dictionary1 = 3
HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Count of Key/value pairs in Dictionary2 = 3
Count of Key/value pairs in Dictionary2 (Updated) = 0

ตัวอย่างที่ 2: ตรวจสอบคุณสมบัติ IsFixedSize IsReadOnly IsSynchronized

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

public class Demo
{
    public static void Main()
    {
        HybridDictionary dict1 = new HybridDictionary();
        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("HybridDictionary1 elements...");
        foreach (DictionaryEntry d in dict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Is the HybridDictionary1 having fixed size? = " + dict1.IsFixedSize);
        Console.WriteLine("If HybridDictionary1 read-only? = " + dict1.IsReadOnly);
        Console.WriteLine("Is HybridDictionary1 synchronized = " + dict1.IsSynchronized);

        HybridDictionary dict2 = new HybridDictionary();
        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("\nHybridDictionary2 elements...");
        foreach (DictionaryEntry d in dict2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        Console.WriteLine("Is HybridDictionary1 equal to HybridDictionary2? = " + (dict1.Equals(dict2)));
        Console.WriteLine("Is the HybridDictionary2 having fixed size? = " + dict2.IsFixedSize);
        Console.WriteLine("If HybridDictionary2 read-only? = " + dict2.IsReadOnly);
        Console.WriteLine("Is HybridDictionary2 synchronized = " + dict2.IsSynchronized);
    }
}

ผลลัพธ์

HybridDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Is the HybridDictionary1 having fixed size? = False
If HybridDictionary1 read-only? = False
Is HybridDictionary1 synchronized = False
HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Is HybridDictionary1 equal to HybridDictionary2? = False
Is the HybridDictionary2 having fixed size? = False
If HybridDictionary2 read-only? = False
Is HybridDictionary2 synchronized = False

สรุป

  • HybridDictionary ปรับโครงสร้างภายในอัตโนมัติ เหมาะกับขนาดไม่แน่นอน
  • ไม่ได้ thread-safe เอง ต้องล็อกด้วย SyncRoot
  • โค้ดตัวอย่างแรกมีบัคเล็กน้อย: พิมพ์ dict1.Count แทน dict2.Count (已在上面修正)