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

สร้าง HybridDictionary ด้วยขนาดเริ่มต้นที่ระบุใน C#

Constructor HybridDictionary(int initialSize) สร้าง HybridDictionary พร้อมกำหนดความจุเริ่มต้น ช่วยลดการ Resize ให้เกิดขึ้นน้อยลงเมื่อทราบขนาดข้อมูลประมาณแล้ว

ตัวอย่างที่ 1: ขนาดเริ่มต้น 5

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

public class Demo
{
    public static void Main()
    {
        // ระบุขนาดเริ่มต้น 5
        HybridDictionary dict = new HybridDictionary(5);
        dict.Add("A", "AB");
        dict.Add("B", "BC");
        dict.Add("C", "DE");
        dict.Add("D", "FG");
        dict.Add("E", "HI");

        Console.WriteLine("คู่คีย์/ค่า...");
        foreach (DictionaryEntry d in dict)
        {
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
    }
}

ผลลัพธ์

คู่คีย์/ค่า...
Key = A, Value = AB
Key = B, Value = BC
Key = C, Value = DE
Key = D, Value = FG
Key = E, Value = HI

ตัวอย่างที่ 2: ขนาดเริ่มต้น 10

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

public class Demo
{
    public static void Main()
    {
        HybridDictionary dict = new HybridDictionary(10);
        dict.Add("1", 100);
        dict.Add("2", 200);
        dict.Add("3", 300);
        dict.Add("4", 400);
        dict.Add("5", 500);
        dict.Add("6", 600);
        dict.Add("7", 700);
        dict.Add("8", 800);
        dict.Add("9", 900);
        dict.Add("10", 1000);

        Console.WriteLine("คู่คีย์/ค่า...");
        foreach (DictionaryEntry d in dict)
        {
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
    }
}

ผลลัพธ์ (ไม่เรียงลำดับ - HybridDictionary ไม่รับประกันลำดับ)

คู่คีย์/ค่า...
Key = 10, Value = 1000
Key = 1, Value = 100
Key = 2, Value = 200
Key = 3, Value = 300
Key = 4, Value = 400
Key = 5, Value = 500
Key = 6, Value = 600
Key = 7, Value = 700
Key = 8, Value = 800
Key = 9, Value = 900

Constructor Overloads ของ HybridDictionary

Constructorคำอธิบาย
HybridDictionary()สร้างว่าง ขนาดเริ่มต้น 0
HybridDictionary(int initialSize)สร้างว่าง ระบุขนาดเริ่มต้น
HybridDictionary(int initialSize, bool caseInsensitive)ระบุขนาดเริ่มต้น และ Case Sensitivity
HybridDictionary(IDictionary)คัดลอกจาก Dictionary อื่น
HybridDictionary(IEqualityComparer)ระบุ Comparer สำหรับ Key

กลไกการทำงานของ HybridDictionary

  • ขนาดเล็ก (≤ 9 รายการ): ใช้ ListDictionary (Linked List) - เร็วกว่าสำหรับข้อมูลนิดหน่อย
  • ขนาดใหญ่ (> 9 รายการ): สลับเป็น Hashtable อัตโนมัติ - เร็วกว่าสำหรับข้อมูลเยอะ
  • การระบุ initialSize ช่วยให้เลือกโครงสร้างที่เหมาะสมตั้งแต่ต้น

ข้อสังเกต

  • HybridDictionary เป็น Non-generic (Legacy) ใช้ object key/value
  • ไม่รับประกันลำดับ (Unordered)
  • รองรับ Key = null ได้ (ต่างจาก Hashtable)
  • แนะนำใช้ Dictionary<TKey, TValue> แทน สำหรับโค้ดใหม่ (Type-Safe, Generic, ประสิทธิภาพดีกว่า)
  • หากต้องการ Case-Insensitive Key ใช้ StringComparer.OrdinalIgnoreCase กับ Dictionary แทน

ตัวอย่าง Dictionary<TKey, TValue> เทียบเท่า

// Generic Dictionary - แนะนำสำหรับโค้ดใหม่
var dict = new Dictionary<string, string>(5); // Capacity 5
dict["A"] = "AB";
dict["B"] = "BC";
// ...

// Case-Insensitive Dictionary
var ciDict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
ciDict["Key"] = "Value";
ciDict["KEY"] = "Other"; // จะแทนที่ Key เดิม (Case-Insensitive)