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

Lấy enumerator duyệt qua ListDictionary trong C#

ListDictionary là một từ điển đơn giản dựa trên danh sách liên kết, phù hợp cho số lượng phần tử nhỏ (thường < 10). Để duyệt qua các phần tử, sử dụng phương thức GetEnumerator() trả về IDictionaryEnumerator.

Ví dụ 1: Duyệt hai ListDictionary khác nhau

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 cặp key-value của ListDictionary2...");
        IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

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 cặp key-value của ListDictionary2...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six

Ví dụ 2: Duyệt đơn giản với enumerator

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

public class Demo
{
    public static void Main()
    {
        ListDictionary dict = new ListDictionary();
        dict.Add("1", "One");
        dict.Add("2", "Two");
        dict.Add("3", "Three");
        dict.Add("4", "Four");
        dict.Add("5", "Five");
        dict.Add("6", "Six");

        Console.WriteLine("Các cặp key-value của ListDictionary...");
        IDictionaryEnumerator demoEnum = dict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

Kết quả

Các cặp key-value của ListDictionary...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six

Cách hiện đại: Sử dụng foreach

// Cách 1: Duyệt DictionaryEntry (cũ)
foreach (DictionaryEntry entry in dict)
{
    Console.WriteLine($"Key = {entry.Key}, Value = {entry.Value}");
}

// Cách 2: Duyệt KeyValuePair (khuyến nghị)
foreach (var kvp in dict)
{
    Console.WriteLine($"Key = {kvp.Key}, Value = {kvp.Value}");
}

// Cách 3: Deconstruction (C# 7.0+)
foreach (var (key, value) in dict)
{
    Console.WriteLine($"Key = {key}, Value = {value}");
}

Đặc điểm của ListDictionary

Đặc điểm Mô tả
Cấu trúc nội bộ Danh sách liên kết đơn (singly linked list)
Hiệu năng tốt nhất Khi số phần tử < 10
Tìm kiếm O(n) - duyệt tuyến tính
Thứ tự Giữ thứ tự thêm vào (insertion order)
Namespace System.Collections.Specialized

Lưu ý quan trọng

  • ListDictionary đã lỗi thời, khuyến nghị dùng Dictionary<TKey, TValue> hoặc OrderedDictionary
  • Không an toàn cho đa luồng (thread-safe)
  • Key không được null, Value có thể null
  • Phù hợp cho collections nhỏ, ít thao tác tìm kiếm