Giới thiệu
Để lấy số lượng cặp khóa/giá trị trong ListDictionary, bạn sử dụng thuộc tính Count. Dưới đây là các ví dụ minh họa.
Ví dụ 1: Ba 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);
}
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ố lượng cặp khóa/giá trị trong Dictionary 2 = " + dict2.Count);
ListDictionary dict3 = new ListDictionary();
dict3 = dict2;
Console.WriteLine("\nListDictionary3 có bằng ListDictionary2 không? = " + (dict3.Equals(dict2)));
dict3.Clear();
Console.WriteLine("Số lượng cặp khóa/giá trị 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ố lượng cặp khóa/giá trị trong Dictionary 2 = 6 ListDictionary3 có bằng ListDictionary2 không? = True Số lượng cặp khóa/giá trị trong Dictionary 3 = 0
Ví dụ 2: Sử dụng IDictionaryEnumerator
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");
Console.WriteLine("Các phần tử của ListDictionary1...");
foreach (DictionaryEntry d in dict1)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Số lượng cặp khóa/giá trị trong Dictionary1 = " + dict1.Count);
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ị của ListDictionary2...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Khóa = " + demoEnum.Key + ", Giá trị = " + demoEnum.Value);
Console.WriteLine("Số lượng cặp khóa/giá trị trong Dictionary2 = " + dict2.Count);
}
}
Kết quả
Các phần tử của ListDictionary1... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing Số lượng cặp khóa/giá trị trong Dictionary1 = 5 Các cặp khóa-giá trị của ListDictionary2... Khóa = 1, Giá trị = One Khóa = 2, Giá trị = Two Khóa = 3, Giá trị = Three Khóa = 4, Giá trị = Four Khóa = 5, Giá trị = Five Khóa = 6, Giá trị = Six Số lượng cặp khóa/giá trị trong Dictionary2 = 6
Lưu ý: ListDictionary phù hợp cho các tập dữ liệu nhỏ (thường < 10 phần tử). Gán một ListDictionary cho biến khác chỉ tạo tham chiếu, không tạo bản sao.