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

ลบรายการทั้งหมดจาก ListDictionary ใน C#

เมธอด Clear() ของ ListDictionary ใช้ลบคู่คีย์-ค่าทั้งหมด ทำให้ Count กลายเป็น 0

ตัวอย่างที่ 1: ลบรายการทั้งหมดและเปรียบเทียบ

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

public class Demo
{
    public static void Main()
    {
        ListDictionary dict1 = new ListDictionary();
        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("ListDictionary1 องค์ประกอบ...");
        foreach (DictionaryEntry d in dict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        ListDictionary dict2 = new ListDictionary();
        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("\nListDictionary2 องค์ประกอบ...");
        foreach (DictionaryEntry d in dict2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("จำนวนคู่คีย์/ค่าใน Dictionary 2 = " + dict2.Count);

        // สร้าง dict3 อ้างอิงไปที่ dict2 เดียวกัน
        ListDictionary dict3 = dict2;
        Console.WriteLine("\nListDictionary3 เท่ากับ ListDictionary2? = " + (dict3.Equals(dict2)));

        // ลบทั้งหมดใน dict3 (ส่งผลต่อ dict2 ด้วย เพราะอ้างอิงเดียวกัน)
        dict3.Clear();

        Console.WriteLine("จำนวนคู่คีย์/ค่าใน Dictionary 3 = " + dict3.Count);
    }
}

ผลลัพธ์

ListDictionary1 องค์ประกอบ...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

ListDictionary2 องค์ประกอบ...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
จำนวนคู่คีย์/ค่าใน Dictionary 2 = 6
ListDictionary3 เท่ากับ ListDictionary2? = True
จำนวนคู่คีย์/ค่าใน Dictionary 3 = 0

ตัวอย่างที่ 2: แสดงจำนวนก่อน/หลัง Clear

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

public class Demo
{
    public static void Main()
    {
        ListDictionary dict1 = new ListDictionary();
        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("ListDictionary1 องค์ประกอบ...");
        foreach (DictionaryEntry d in dict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("จำนวนคู่คีย์/ค่าใน Dictionary 1 = " + dict1.Count);
    }
}

ผลลัพธ์

ListDictionary1 องค์ประกอบ...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
จำนวนคู่คีย์/ค่าใน Dictionary 1 = 6

ข้อสังเกตสำคัญ

  • Clear() ลบทุกคู่คีย์-ค่า ทำให้ Count = 0
  • ในตัวอย่างที่ 1 dict3 = dict2 ทำให้ทั้งสองตัวแปรอ้างอิง ออบเจ็กต์เดียวกัน การ Clear() ผ่าน dict3 ส่งผลต่อ dict2 ด้วย
  • หากต้องการคัดลอกจริง ต้องสร้างออบเจ็กต์ใหม่และ Add ทีละคู่ หรือใช้ LINQ: new ListDictionary(dict2.Cast<DictionaryEntry>().ToDictionary(d => d.Key, d => d.Value)) (ต้องแปลงประเภท)
  • ListDictionary เป็น Legacy ควรใช้ Dictionary<TKey, TValue> แทนในโค้ดใหม่