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

Lấy enumerator duyệt StringDictionary trong C#

Giới thiệu

StringDictionary (namespace System.Collections.Specialized) là từ điển chuỗi-chuỗi cũ, không generic. Dùng GetEnumerator() trả về IEnumerator để duyệt qua các DictionaryEntry.

Ví dụ 1: Duyệt bằng foreach (ẩn enumerator)

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

public class Demo
{
    public static void Main()
    {
        StringDictionary dict = new StringDictionary();
        dict.Add("A", "John");
        dict.Add("B", "Andy");
        dict.Add("C", "Tim");
        dict.Add("D", "Ryan");
        dict.Add("E", "Kevin");
        dict.Add("F", "Katie");
        dict.Add("G", "Brad");

        Console.WriteLine("Duyệt bằng foreach:");
        foreach (DictionaryEntry entry in dict)
        {
            Console.WriteLine($"Key = {entry.Key}, Value = {entry.Value}");
        }
    }
}

Kết quả

Duyệt bằng foreach:
Key = A, Value = John
Key = B, Value = Andy
Key = C, Value = Tim
Key = D, Value = Ryan
Key = E, Value = Kevin
Key = F, Value = Katie
Key = G, Value = Brad

Ví dụ 2: Duyệt thủ công với IEnumerator

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

public class Demo
{
    public static void Main()
    {
        StringDictionary dict = new StringDictionary();
        dict.Add("1", "A");
        dict.Add("2", "B");
        dict.Add("3", "C");
        dict.Add("4", "D");
        dict.Add("5", "E");

        Console.WriteLine("Duyệt thủ công IEnumerator:");
        IEnumerator enumerator = dict.GetEnumerator();
        while (enumerator.MoveNext())
        {
            DictionaryEntry entry = (DictionaryEntry)enumerator.Current;
            Console.WriteLine($"Key = {entry.Key}, Value = {entry.Value}");
        }
    }
}

Kết quả

Duyệt thủ công IEnumerator:
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E

Ví dụ 3: StringDictionary khác

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

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

        IEnumerator enumr = dict.GetEnumerator();
        while (enumr.MoveNext())
        {
            DictionaryEntry de = (DictionaryEntry)enumr.Current;
            Console.WriteLine($"Key = {de.Key}, Value = {de.Value}");
        }
    }
}

Lưu ý quan trọng

  • StringDictionary **lỗi thời** (legacy). Hãy dùng Dictionary<string, string> (generic, nhanh hơn, type-safe).
  • Không thread-safe. Cần lock thủ công hoặc dùng ConcurrentDictionary.
  • Key không phân biệt chữ hoa/thường (case-insensitive) nội bộ.
  • Enumerator **không hỗ trợ** Remove, chỉ đọc.
  • Thứ tự duyệt **không đảm bảo** (hash-based).

So sánh nhanh

Đặc điểmStringDictionaryDictionary<string,string>
GenericKhôngCó
Key case-sensitiveKhông (insensitive)Có (mặc định)
Hiệu năngThấp hơn (boxing)Cao
Thread-safeKhôngKhông (cần ConcurrentDictionary)