Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Xóa tất cả mục khỏi StringDictionary trong C#

Giới thiệu

Phương thức Clear() của StringDictionary xóa tất cả các cặp khóa/giá trị, đưa Count về 0. Lớp này nằm trong System.Collections.Specialized và là collection legacy, khuyến nghị dùng Dictionary<string, string>.

Ví dụ 1

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("Các phần tử StringDictionary1...");
        foreach (DictionaryEntry d in strDict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("StringDictionary1 có khóa 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("\nCác phần tử StringDictionary2...");
        foreach (DictionaryEntry d in strDict2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("Số cặp khóa/giá trị trong StringDictionary2 = " + strDict2.Count);
        strDict2.Clear();
        Console.WriteLine("Số cặp khóa/giá trị (sau Clear) = " + strDict2.Count);
    }
}

Kết quả

Các phần tử StringDictionary1...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary1 có khóa G? True

Các phần tử StringDictionary2...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Số cặp khóa/giá trị trong StringDictionary2 = 7
Số cặp khóa/giá trị (sau Clear) = 0

Ví dụ 2

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("Các phần tử StringDictionary...");
        foreach (DictionaryEntry d in strDict)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("Số cặp khóa/giá trị = " + strDict.Count);
        strDict.Clear();
        Console.WriteLine("Số cặp khóa/giá trị (sau Clear) = " + strDict.Count);
    }
}

Kết quả

Các phần tử StringDictionary...
a John
b Andy
c Tim
d Ryan
Số cặp khóa/giá trị = 4
Số cặp khóa/giá trị (sau Clear) = 0

Lưu ý quan trọng

  • StringDictionary không phân biệt chữ hoa/chữ thường cho khóa (khóa lưu dưới dạng chữ thường).
  • Clear() xóa tất cả mục, độ phức tạp O(n).
  • Trong .NET hiện đại, sử dụng Dictionary<string, string> thay thế.
  • Dictionary<string, string> cũng có phương thức Clear() tương tự.