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

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

บทความนี้สาธิตวิธีลบรายการ (entries) ทั้งหมดออกจาก StringDictionary โดยใช้เมธอด Clear() ใน C#

ตัวอย่างที่ 1: ล้าง StringDictionary และตรวจสอบ Count

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 d in strDict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("Does StringDictionary1 has key G? " + strDict1.ContainsKey("G"));

        StringDictionary strDict2 = new StringDictionary();
        strDict2.Add("A", "John");
        strDict2.Add("B", "Andy");
        strDict2.Add("C", "Tim");
        strDict2.Add("D", "Ryan");
        strDict2.Add("E", "Kevin");
        strDict2.Add("F", "Katie");
        strDict2.Add("G", "Brad");

        Console.WriteLine("\nStringDictionary2 elements...");
        foreach (DictionaryEntry d in strDict2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("Count of key/value pairs in StringDictionary2 = " + strDict2.Count);
        strDict2.Clear();
        Console.WriteLine("Count of key/value pairs in StringDictionary2 (updated) = " + strDict2.Count);
    }
}

ผลลัพธ์

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Does StringDictionary1 has key G? True
StringDictionary2 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Count of key/value pairs in StringDictionary2 = 7
Count of key/value pairs in StringDictionary2 (updated) = 0

ตัวอย่างที่ 2: StringDictionary ขนาดเล็ก ล้างและตรวจสอบ Count

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

public class Demo
{
    public static void Main()
    {
        StringDictionary strDict = new StringDictionary();
        strDict.Add("A", "John");
        strDict.Add("B", "Andy");
        strDict.Add("C", "Tim");
        strDict.Add("D", "Ryan");

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

        Console.WriteLine("Count of key/value pairs in StringDictionary = " + strDict.Count);
        strDict.Clear();
        Console.WriteLine("Count of key/value pairs in StringDictionary (updated) = " + strDict.Count);
    }
}

ผลลัพธ์

StringDictionary elements...
a John
b Andy
c Tim
d Ryan
Count of key/value pairs in StringDictionary = 4
Count of key/value pairs in StringDictionary (updated) = 0

สรุป

  • ใช้ Clear() เพื่อลบรายการคีย์-ค่าทั้งหมดออกจาก StringDictionary — ทำให้ Count = 0
  • StringDictionary อยู่ใน System.Collections.Specialized — เป็นคอลเลกชัน non-generic แบบเก่า (legacy)
  • คีย์และค่าต้องเป็น string เท่านั้น — คีย์จะถูกแปลงเป็น lowercase อัตโนมัติ (case-insensitive)
  • แนะนำให้ใช้ Dictionary<string, string> แทน ในโค้ดใหม่ — มี type safety, generic, ประสิทธิภาพดีกว่า, และไม่บังคับ lowercase keys
  • เมธอดอื่นๆ: Add(), Remove(key), ContainsKey(), ContainsValue(), Item[key] (indexer)

ตัวอย่างใช้ Dictionary<string, string> แทน (แนะนำ)

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        var dict = new Dictionary<string, string>();
        dict.Add("A", "John");
        dict.Add("B", "Andy");
        // คีย์ case-sensitive โดยค่าเริ่มต้น

        Console.WriteLine("Count before clear: " + dict.Count); // 2
        dict.Clear();
        Console.WriteLine("Count after clear: " + dict.Count);  // 0
    }
}