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

วิธีลบรายการตามคีย์ที่ระบุจาก StringDictionary ใน C#

บทความนี้สาธิตวิธีการลบรายการ (entry) ตามคีย์ที่ระบุจาก StringDictionary โดยใช้ method Remove(string key)

ตัวอย่างที่ 1: ลบคีย์ "D" และแสดงก่อนหลัง

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 key-value pairs...");
        foreach (DictionaryEntry de in strDict1)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }

        strDict1.Remove("D");

        Console.WriteLine("\nStringDictionary1 key-value pairs after removing a key...");
        foreach (DictionaryEntry de in strDict1)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }

        StringDictionary strDict2 = new StringDictionary();
        strDict2.Add("1", "A");
        strDict2.Add("2", "B");
        strDict2.Add("3", "C");
        strDict2.Add("4", "D");
        strDict2.Add("5", "E");

        Console.WriteLine("\nStringDictionary2 key-value pairs...");
        IEnumerator demoEnum = strDict2.GetEnumerator();
        DictionaryEntry d;
        while (demoEnum.MoveNext())
        {
            d = (DictionaryEntry)demoEnum.Current;
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
    }
}

ผลลัพธ์

StringDictionary1 key-value pairs...
A John
B Andy
C Tim
D Ryan
E Kevin
F Katie
G Brad

StringDictionary1 key-value pairs after removing a key...
A John
B Andy
C Tim
E Kevin
F Katie
G Brad

StringDictionary2 key-value pairs...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E

ตัวอย่างที่ 2: ลบหลายคีย์ติดต่อกัน

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

public class Demo
{
    public static void Main()
    {
        StringDictionary strDict1 = new StringDictionary();
        strDict1.Add("A", "One");
        strDict1.Add("B", "Two");
        strDict1.Add("C", "Three");
        strDict1.Add("D", "Four");
        strDict1.Add("E", "Five");

        Console.WriteLine("StringDictionary1 key-value pairs...");
        foreach (DictionaryEntry de in strDict1)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }

        strDict1.Remove("B");
        strDict1.Remove("C");

        Console.WriteLine("\nStringDictionary1 key-value pairs after removing a key...");
        foreach (DictionaryEntry de in strDict1)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }
    }
}

ผลลัพธ์

StringDictionary1 key-value pairs...
A One
B Two
C Three
D Four
E Five

StringDictionary1 key-value pairs after removing a key...
A One
D Four
E Five

คำอธิบายเพิ่มเติม

  • Remove(string key) ลบรายการที่มีคีย์ตรงกับที่ระบุ คืนค่า void
  • ถ้าคีย์ไม่มีอยู่ จะไม่เกิด error - silent ignore
  • Key ไม่สนใจตัวพิมพ์ใหญ่เล็ก (case-insensitive) - "D" และ "d" เหมือนกัน
  • StringDictionary เป็น legacy class - แนะนำใช้ Dictionary<string, string> แทน
  • Dictionary<string, string> มี Remove คืนค่า bool บอกว่าลบสำเร็จหรือไม่
  • ใช้ foreach กับ DictionaryEntry สำหรับการวนลูป (key เป็น string, value เป็น string)