คุณสมบัติ SortedDictionary<TKey, TValue>.Keys ใน C# ใช้ดึงคอลเลกชันที่มีเฉพาะคีย์ (keys) จาก SortedDictionary โดยคีย์จะถูกเรียงลำดับอัตโนมัติจากน้อยไปมาก
รูปแบบไวยากรณ์ (Syntax)
public System.Collections.Generic.SortedDictionary<TKey,TValue>.KeyCollection Keys { get; }
ตัวอย่างที่ 1: การจัดการ Keys และ Values พร้อมการอัปเดตค่า
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("SortedDictionary key-value pairs...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Count of SortedDictionary key-value pairs = " + sortedDict.Count);
// เพิ่มข้อมูลใหม่
sortedDict.Add(7, "Seven");
sortedDict.Add(8, "Eight");
Console.WriteLine("\nSortedDictionary key-value pairs...UPDATED");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Count of SortedDictionary key-value pairs (UPDATED) = " + sortedDict.Count);
// เข้าถึงและแก้ไขค่าผ่าน Indexer (Item property)
Console.WriteLine("Value at key 5 = " + sortedDict[5]);
sortedDict[5] = "Crossover";
Console.WriteLine("Value at key 5 (updated) = " + sortedDict[5]);
// ดึง Keys collection
SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
Console.WriteLine("\nKeys...");
foreach (int i in keyColl)
{
Console.WriteLine(i);
}
}
}
ผลลัพธ์
SortedDictionary key-value pairs... 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 Count of SortedDictionary key-value pairs = 6 SortedDictionary key-value pairs...UPDATED 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 Count of SortedDictionary key-value pairs (UPDATED) = 8 Value at key 5 = Hatchback Value at key 5 (updated) = Crossover Keys... 1 2 3 4 5 6 7 8
ตัวอย่างที่ 2: ตรวจสอบคีย์และแสดงรายการ 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("SortedDictionary key-value pairs...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("\nThe SortedDictionary has the key 200? = " + sortedDict.ContainsKey(200));
SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
Console.WriteLine("\nKeys...");
foreach (int i in keyColl)
{
Console.WriteLine(i);
}
}
}
ผลลัพธ์
SortedDictionary key-value pairs... Key = 100, Value = Mobile Key = 200, Value = Laptop Key = 300, Value = Desktop Key = 400, Value = Speakers Key = 500, Value = Headphone Key = 600, Value = Earphone The SortedDictionary has the key 200? = True Keys... 100 200 300 400 500 600
สรุป
Keysคืนค่าKeyCollectionที่ implementICollection<TKey>- คีย์ถูกจัดเก็บในลำดับที่เรียงแล้ว (sorted) ตลอดเวลา
- สามารถวนลูปด้วย
foreachหรือแปลงเป็นList<TKey>/Arrayได้ - KeyCollection เป็น read-only view ไม่สามารถเพิ่ม/ลบคีย์ผ่าน collection นี้ได้โดยตรง
- ใช้ร่วมกับ
ContainsKey()เพื่อตรวจสอบก่อนเข้าถึง