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

Lớp ListDictionary trong C#

Lớp ListDictionary triển khai IDictionary bằng danh sách liên kết đơn. Phù hợp cho các tập hợp nhỏ (dưới 10 phần tử).

Thuộc tính

STTThuộc tính & Mô tả
1Count: Số cặp khóa/giá trị.
2IsFixedSize: Cho biết kích thước có cố định không.
3IsReadOnly: Cho biết có chỉ đọc không.
4IsSynchronized: Cho biết có đồng bộ (thread-safe) không.
5Item[Object]: Lấy/đặt giá trị theo khóa.
6Keys: Tập hợp các khóa.
7SyncRoot: Đối tượng đồng bộ hóa truy cập.
8Values: Tập hợp các giá trị.

Phương thức

STTPhương thức & Mô tả
1Add(Object, Object): Thêm cặp khóa/giá trị.
2Clear(): Xóa tất cả mục.
3Contains(Object): Kiểm tra khóa tồn tại.
4CopyTo(Array, Int32): Sao chép vào mảng.
5GetEnumerator(): Trả về IDictionaryEnumerator.

Ví dụ 1: Kiểm tra thuộc tí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ử ListDictionary1...");
        foreach(DictionaryEntry d in dict1) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("Kích thước cố định? = " + dict1.IsFixedSize);
        Console.WriteLine("Chỉ đọc? = " + dict1.IsReadOnly);
        Console.WriteLine("Đồng bộ? = " + dict1.IsSynchronized);
        Console.WriteLine("Có khóa M? = " + dict1.Contains("M"));
        
        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 khóa-giá trị ListDictionary2...");
        IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        
        Console.WriteLine("Kích thước cố định? = " + dict2.IsFixedSize);
        Console.WriteLine("Chỉ đọc? = " + dict2.IsReadOnly);
        Console.WriteLine("Đồng bộ? = " + dict2.IsSynchronized);
        Console.WriteLine("Có khóa 5? = " + dict2.Contains("5"));
    }
}

Kết quả

Các phần tử ListDictionary1...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Kích thước cố định? = False
Chỉ đọc? = False
Đồng bộ? = False
Có khóa M? = False

Các cặp khóa-giá trị 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
Kích thước cố định? = False
Chỉ đọc? = False
Đồng bộ? = False
Có khóa 5? = True

Ví dụ 2: So sánh hai 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("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("\nListDictionary2...");
        foreach(DictionaryEntry d in dict2)
            Console.WriteLine(d.Key + " " + d.Value);
        
        ListDictionary dict3 = new ListDictionary();
        dict3 = dict2;
        
        Console.WriteLine("\nListDictionary3 bằng ListDictionary2? = " + (dict3.Equals(dict2)));
    }
}

Kết quả

ListDictionary1...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

ListDictionary2...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six

ListDictionary3 bằng ListDictionary2? = True