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

Lấy tập hợp giá trị trong StringDictionary trong C#

StringDictionary cung cấp thuộc tính Keys và indexer để truy xuất giá trị. Bạn có thể sao chép khóa vào mảng và lặp để lấy cặp khóa/giá trị.

Ví dụ 1: Lấy khóa và giá trị, kiểm tra khóa

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

public class Demo
{
    public static void Main()
    {
        StringDictionary strDict1 = new StringDictionary();
        strDict1.Add("S", "Home Appliances");
        strDict1.Add("T", "Smart Wearable Tech");
        strDict1.Add("U", "Electronics");
        strDict1.Add("V", "Toys");
        strDict1.Add("W", "Books");
        strDict1.Add("X", "Accessories");

        Console.WriteLine("Các phần tử StringDictionary...");
        foreach (DictionaryEntry d in strDict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("\nTập khóa và giá trị...");
        String[] keyArr = new String[strDict1.Count];
        strDict1.Keys.CopyTo(keyArr, 0);
        for (int i = 0; i < strDict1.Count; i++)
        {
            Console.WriteLine("Khóa " + (i+1) + " = " + keyArr[i] + ", Giá trị " + (i+1) + " = " + strDict1[keyArr[i]]);
        }

        Console.WriteLine("\nCó khóa 'B'? " + strDict1.ContainsKey("B"));
    }
}

Kết quả

Các phần tử StringDictionary...
x Accessories
s Home Appliances
t Smart Wearable Tech
u Electronics
v Toys
w Books
Tập khóa và giá trị...
Khóa 1 = x, Giá trị 1 = Accessories
Khóa 2 = s, Giá trị 2 = Home Appliances
Khóa 3 = t, Giá trị 3 = Smart Wearable Tech
Khóa 4 = u, Giá trị 4 = Electronics
Khóa 5 = v, Giá trị 5 = Toys
Khóa 6 = w, Giá trị 6 = Books
Có khóa 'B'? False

Ví dụ 2: Khóa chữ hoa được chuyển thành chữ thường

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ử StringDictionary...");
        foreach (DictionaryEntry d in strDict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("\nTập khóa và giá trị...");
        String[] keyArr = new String[strDict1.Count];
        strDict1.Keys.CopyTo(keyArr, 0);
        for (int i = 0; i < strDict1.Count; i++)
        {
            Console.WriteLine("Khóa " + (i+1) + " = " + keyArr[i] + ", Giá trị " + (i+1) + " = " + strDict1[keyArr[i]]);
        }

        Console.WriteLine("\nCó khóa 'B'? " + strDict1.ContainsKey("B"));
    }
}

Kết quả

Các phần tử StringDictionary...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Tập khóa và giá trị...
Khóa 1 = a, Giá trị 1 = John
Khóa 2 = b, Giá trị 2 = Andy
Khóa 3 = c, Giá trị 3 = Tim
Khóa 4 = d, Giá trị 4 = Ryan
Khóa 5 = e, Giá trị 5 = Kevin
Khóa 6 = f, Giá trị 6 = Katie
Khóa 7 = g, Giá trị 7 = Brad
Có khóa 'B'? True

Lưu ý: Khóa không phân biệt chữ hoa/thường. StringDictionary đã lỗi thời, hãy dùng Dictionary<string, string> với StringComparer.OrdinalIgnoreCase.