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

Xóa tất cả mục từ ListDictionary trong C#

Để xóa tất cả các cặp key-value khỏi ListDictionary, bạn sử dụng phương thức Clear(). Phương thức này loại bỏ tất cả các mục và đặt Count về 0.

Ví dụ 1: Xóa tất cả từ ListDictionary và so sánh

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

        Console.WriteLine("Số cặp key/value trong Dictionary 2 = " + dict2.Count);

        // Tạo dict3 tham chiếu đến dict2
        ListDictionary dict3 = dict2;
        Console.WriteLine("\nListDictionary3 bằng ListDictionary2? = " + dict3.Equals(dict2));

        // Xóa tất cả từ dict3 (cũng xóa dict2 vì cùng tham chiếu)
        dict3.Clear();

        Console.WriteLine("Số cặp key/value trong Dictionary 3 = " + dict3.Count);
    }
}

Kết quả

Các phần tử của ListDictionary1...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

Các phần tử của ListDictionary2...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Số cặp key/value trong Dictionary 2 = 6
ListDictionary3 bằng ListDictionary2? = True
Số cặp key/value trong Dictionary 3 = 0

Ví dụ 2: Tạo và hiển thị ListDictionary

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

        Console.WriteLine("Số cặp key/value trong Dictionary 1 = " + dict1.Count);
    }
}

Kết quả

Các phần tử của ListDictionary1...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Số cặp key/value trong Dictionary 1 = 6

Lưu ý quan trọng về tham chiếu

⚠️ Cảnh báo: Trong ví dụ 1, ListDictionary dict3 = dict2; tạo một tham chiếu đến cùng một đối tượng, không phải bản sao. Khi gọi dict3.Clear(), nó cũng xóa dữ liệu của dict2.
// Để tạo bản sao thực sự:
ListDictionary dict3 = new ListDictionary();
foreach (DictionaryEntry entry in dict2)
{
    dict3.Add(entry.Key, entry.Value);
}
// Hoặc dùng constructor (nếu có)
// ListDictionary không có copy constructor

Các phương thức liên quan

Phương thức Mô tả
Clear() Xóa tất cả key-value pairs
Remove(key) Xóa một cặp theo key
Add(key, value) Thêm cặp key-value
Contains(key) Kiểm tra key tồn tại

Lưu ý

  • ListDictionary nằm trong System.Collections.Specialized
  • Đã lỗi thời - khuyến nghị dùng Dictionary<TKey, TValue> hoặc OrderedDictionary
  • Phù hợp cho collections nhỏ (< 10 phần tử)
  • Giữ thứ tự thêm vào (insertion order)
  • Không type-safe (key/value là object)