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

คลาส OrderedDictionary ใน C#

ภาพรวม

OrderedDictionary แทนคอลเลกชันคู่คีย์/ค่า ที่เข้าถึงได้ทั้งด้วยคีย์และดัชนี รักษาลำดับการเพิ่มข้อมูล

Property หลัก

Propertyคำอธิบาย
Countจำนวนคู่คีย์/ค่า
IsReadOnlyอ่านอย่างเดียวหรือไม่ (ปกติ false)
Item[Int32]ค่าที่ดัชนีที่ระบุ
Item[Object]ค่าที่คีย์ที่ระบุ
KeysICollection ของคีย์ทั้งหมด
ValuesICollection ของค่าทั้งหมด

Method หลัก

Methodคำอธิบาย
Add(Object, Object)เพิ่มคู่คีย์/ค่า (คีย์ไม่ซ้ำ)
AsReadOnly()คืนสำเนา read-only
Clear()ล้างข้อมูลทั้งหมด
Contains(Object)ตรวจสอบว่ามีคีย์หรือไม่
CopyTo(Array, Int32)คัดลอกไปอาร์เรย์ เริ่มดัชนีที่ระบุ
Equals(Object)เปรียบเทียบออบเจกต์
GetEnumerator()คืน IDictionaryEnumerator

ตัวอย่างที่ 1: นับจำนวนและล้างข้อมูล

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

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

        Console.WriteLine("OrderedDictionary elements...");
        foreach (DictionaryEntry d in dict)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
        dict.Clear();
        Console.WriteLine("Count of elements in OrderedDictionary (Updated) = " + dict.Count);
    }
}

ผลลัพธ์

OrderedDictionary elements...
A Home Appliances
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of elements in OrderedDictionary = 6
Count of elements in OrderedDictionary (Updated) = 0

ตัวอย่างที่ 2: เพิ่มข้อมูลและล้าง (ซ้ำเพื่อสาธิต)

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

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

        Console.WriteLine("OrderedDictionary elements...");
        foreach (DictionaryEntry d in dict)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
        dict.Clear();
        Console.WriteLine("Count of elements in OrderedDictionary (Updated) = " + dict.Count);
    }
}

ผลลัพธ์

OrderedDictionary elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Count of elements in OrderedDictionary = 6
Count of elements in OrderedDictionary (Updated) = 0

สรุป

  • รักษาลำดับการเพิ่ม (insertion order) เป็นคุณสมบัติหลัก
  • เข้าถึงแบบ index-based และ key-based
  • ไม่จัดเรียงอัตโนมัติ (ต่างจาก SortedList)
  • ใช้ AsReadOnly() สร้างมุมมองป้องกันการแก้ไข