คุณสมบัติ StringDictionary.Keys ใน C# ใช้สำหรับดึงคอลเลกชันของ Key ทั้งหมด ใน StringDictionary คืนค่า ICollection ที่สามารถวนลูปหรือคัดลอกไปอาร์เรย์ได้
ตัวอย่างที่ 1: StringDictionary สองอัน — ดึง Keys และตรวจสอบ ContainsKey
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("U", "Electronics");
strDict1.Add("V", "Toys");
strDict1.Add("W", "Books");
strDict1.Add("X", "Accessories");
Console.WriteLine("StringDictionary1 elements...");
foreach (DictionaryEntry d in strDict1)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Does StringDictionary1 have key G? " + strDict1.ContainsKey("G"));
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("A", "John");
strDict2.Add("B", "Andy");
strDict2.Add("C", "Tim");
strDict2.Add("D", "Ryan");
strDict2.Add("E", "Kevin");
strDict2.Add("F", "Katie");
strDict2.Add("G", "Brad");
Console.WriteLine("\nStringDictionary2 elements...");
foreach (DictionaryEntry d in strDict2)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nCollection of keys (StringDictionary2)...");
string[] keyArr = new string[strDict2.Count];
strDict2.Keys.CopyTo(keyArr, 0);
for (int i = 0; i < strDict2.Count; i++)
{
Console.WriteLine("Key " + (i + 1) + " = " + keyArr[i]);
}
Console.WriteLine("\nIs Dictionary2 equal to Dictionary1? = " + strDict2.Equals(strDict1));
Console.WriteLine("Does StringDictionary2 have key B? " + strDict2.ContainsKey("B"));
}
}
ผลลัพธ์
StringDictionary1 elements... X Accessories U Electronics V Toys W Books Does StringDictionary1 have key G? False StringDictionary2 elements... A John B Andy C Tim D Ryan E Kevin F Katie G Brad Collection of keys (StringDictionary2)... Key 1 = A Key 2 = B Key 3 = C Key 4 = D Key 5 = E Key 6 = F Key 7 = G Is Dictionary2 equal to Dictionary1? = False Does StringDictionary2 have key B? True
ตัวอย่างที่ 2: ดึง Keys และ Values พร้อมกัน
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("StringDictionary elements...");
foreach (DictionaryEntry d in strDict1)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nCollection of keys and values...");
string[] keyArr = new string[strDict1.Count];
strDict1.Keys.CopyTo(keyArr, 0);
for (int i = 0; i < strDict1.Count; i++)
{
Console.WriteLine("Key " + (i + 1) + " = " + keyArr[i] + ", Value " + (i + 1) + " = " + strDict1[keyArr[i]]);
}
Console.WriteLine("\nDoes StringDictionary have key B? " + strDict1.ContainsKey("B"));
}
}
ผลลัพธ์
StringDictionary elements... X Accessories S Home Appliances T Smart Wearable Tech U Electronics V Toys W Books Collection of keys and values... Key 1 = X, Value 1 = Accessories Key 2 = S, Value 2 = Home Appliances Key 3 = T, Value 3 = Smart Wearable Tech Key 4 = U, Value 4 = Electronics Key 5 = V, Value 5 = Toys Key 6 = W, Value 6 = Books Does StringDictionary have key B? False
ข้อสังเกตสำคัญ
StringDictionary(ในSystem.Collections.Specialized) เป็น non-generic legacy class — Key และ Value ต้องเป็น string เสมอKeysและValuesคืนICollection— ใช้CopyTo()คัดลอกไปอาร์เรย์ หรือforeachวนลูปโดยตรง- ลำดับของ Keys ไม่ได้รับประกัน (ขึ้นกับการแฮชภายใน) — ต่างจาก OrderedDictionary
Equals()ของ StringDictionary ใช้ reference equality — ไม่เปรียบเทียบเนื้อหา- ในโปรเจกต์ใหม่ ใช้
Dictionary<string, string>แทน (Generic, type-safe, ประสิทธิภาพดีกว่า) ContainsKey()เป็น O(1) — เร็วมากสำหรับการตรวจสอบ Key