Thuộc tính SortedDictionary<TKey, TValue>.Keys trong C# được dùng để lấy một tập hợp (collection) chứa các khóa (keys) trong SortedDictionary. Tập hợp trả về là kiểu SortedDictionary<TKey, TValue>.KeyCollection, được sắp xếp tăng dần theo key.
Cú pháp
public System.Collections.Generic.SortedDictionary.KeyCollection Keys { get; }
Ví dụ 1: Thêm, cập nhật, liệt kê Keys
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(1, "SUV");
sortedDict.Add(2, "MUV");
sortedDict.Add(3, "Utility Vehicle");
sortedDict.Add(4, "AUV");
sortedDict.Add(5, "Hatchback");
sortedDict.Add(6, "Convertible");
Console.WriteLine("Các cặp key-value...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng cặp = " + sortedDict.Count);
// Thêm thêm
sortedDict.Add(7, "Seven");
sortedDict.Add(8, "Eight");
Console.WriteLine("\nCác cặp key-value SAU KHI CẬP NHẬT...");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Số lượng cặp (cập nhật) = " + sortedDict.Count);
// Truy cập và cập nhật qua indexer
Console.WriteLine("Giá trị key 5 = " + sortedDict[5]);
sortedDict[5] = "Crossover";
Console.WriteLine("Giá trị key 5 (cập nhật) = " + sortedDict[5]);
// Lấy tập hợp Keys
SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
Console.WriteLine("\nDanh sách Keys...");
foreach (int i in keyColl)
{
Console.WriteLine(i);
}
}
}
Kết quả
Các cặp key-value... Key = 1, Value = SUV Key = 2, Value = MUV Key = 3, Value = Utility Vehicle Key = 4, Value = AUV Key = 5, Value = Hatchback Key = 6, Value = Convertible Số lượng cặp = 6 Các cặp key-value SAU KHI CẬP NHẬT... Key = 1, Value = SUV Key = 2, Value = MUV Key = 3, Value = Utility Vehicle Key = 4, Value = AUV Key = 5, Value = Hatchback Key = 6, Value = Convertible Key = 7, Value = Seven Key = 8, Value = Eight Số lượng cặp (cập nhật) = 8 Giá trị key 5 = Hatchback Giá trị key 5 (cập nhật) = Crossover Danh sách Keys... 1 2 3 4 5 6 7 8
Ví dụ 2: Kiểm tra ContainsKey và liệt kê Keys
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(100, "Mobile");
sortedDict.Add(200, "Laptop");
sortedDict.Add(300, "Desktop");
sortedDict.Add(400, "Speakers");
sortedDict.Add(500, "Headphone");
sortedDict.Add(600, "Earphone");
Console.WriteLine("Các cặp key-value...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("\nCó key 200? = " + sortedDict.ContainsKey(200));
SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
Console.WriteLine("\nDanh sách Keys...");
foreach (int i in keyColl)
{
Console.WriteLine(i);
}
}
}
Kết quả
Các cặp key-value... Key = 100, Value = Mobile Key = 200, Value = Laptop Key = 300, Value = Desktop Key = 400, Value = Speakers Key = 500, Value = Headphone Key = 600, Value = Earphone Có key 200? = True Danh sách Keys... 100 200 300 400 500 600
Đặc điểm của KeyCollection
- Keys luôn được sắp xếp tăng dần (theo IComparer<TKey> mặc định hoặc custom).
- Là view động: thay đổi SortedDictionary phản ánh ngay.
- Hỗ trợ
Count,Contains,CopyTo,GetEnumerator. - Không hỗ trợ indexer
[i](dùng LINQElementAt(i)hoặcToList()).
Chuyển sang List/Array
ListkeyList = sortedDict.Keys.ToList(); int[] keyArray = sortedDict.Keys.ToArray();
So sánh Keys vs Values
| Thuộc tính | Kiểu trả về | Thứ tự |
|---|---|---|
Keys |
KeyCollection | Tăng dần theo key |
Values |
ValueCollection | Theo thứ tự key tương ứng |