คลาส StringDictionay ใช้ตารางแฮชที่มีคีย์และค่าที่พิมพ์อย่างแน่นหนาให้เป็นสตริงแทนที่จะเป็นอ็อบเจ็กต์
ต่อไปนี้เป็นคุณสมบัติของคลาส StringDictionary -
| ซีเนียร์ | คุณสมบัติ &คำอธิบาย |
|---|---|
| 1 | นับ รับจำนวนคู่คีย์/ค่าใน StringDictionary |
| 2 | ซิงโครไนซ์ รับค่าที่ระบุว่าการเข้าถึง StringDictionary มีการซิงโครไนซ์หรือไม่ (ความปลอดภัยของเธรด) |
| 3 | tem[สตริง] รับหรือตั้งค่าที่เกี่ยวข้องกับคีย์ที่ระบุ |
| 4 | กุญแจ รับชุดของคีย์ใน StringDictionary.. |
| 5 | SyncRoot รับอ็อบเจ็กต์ที่สามารถใช้เพื่อซิงโครไนซ์การเข้าถึง StringDictionary |
| 6 | ค่า รับค่าคอลเลกชันใน StringDictionary |
ต่อไปนี้เป็นวิธีการบางอย่างของคลาส StringDictionary -
| ซีเนียร์ | วิธีการ &คำอธิบาย |
|---|---|
| 1 | เพิ่ม (สตริง สตริง) เพิ่มรายการด้วยคีย์และค่าที่ระบุลงใน StringDictionary |
| 2 | ล้าง() ลบรายการทั้งหมดออกจาก StringDictionary |
| 3 | ContainsKey(สตริง) กำหนดว่า StringDictionary มีเฉพาะหรือไม่ |
| 4 | ContainsValue(สตริง) กำหนดว่า StringDictionary มีค่าเฉพาะหรือไม่ |
| 5 | CopyTo(Array, Int32) คัดลอกค่าพจนานุกรมสตริงไปยังอินสแตนซ์ Array แบบหนึ่งมิติที่ดัชนีที่ระบุ |
| 6 | เท่ากับ (วัตถุ) กำหนดว่าวัตถุที่ระบุเท่ากับวัตถุปัจจุบันหรือไม่ (สืบทอดจากวัตถุ) |
ตัวอย่าง
เรามาดูตัวอย่างกัน −
เพื่อตรวจสอบว่าสองวัตถุ StringDictionary เท่ากันหรือไม่ รหัสจะเป็นดังนี้ -
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
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");
StringDictionary strDict3 = new StringDictionary();
strDict3 = strDict2;
Console.WriteLine("Is Dictionary2 equal to Dictionary3? = "+strDict2.Equals(strDict3));
Console.WriteLine("Is Dictionary1 equal to Dictionary3? = "+strDict1.Equals(strDict3));
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Is Dictionary2 equal to Dictionary3? = True Is Dictionary1 equal to Dictionary3? = False
ตัวอย่าง
เพื่อตรวจสอบว่า StringDictionary ซิงโครไนซ์หรือไม่ รหัสจะเป็นดังนี้ -
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 elements...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("\nStringDictionary2 key-value pairs...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Is the StringDictionary2 synchronized? = "+strDict2.IsSynchronized);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E Is the StringDictionary2 synchronized? = False