Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

คลาส StringDictionary ใน C#

ภาพรวม

StringDictionary (ใน System.Collections.Specialized) เป็นแฮชเทเบิลที่คีย์และค่าเป็นสตริงอย่างเคร่งครัด (strongly-typed) ใช้งานง่ายกว่า Hashtable สำหรับข้อมูลสตริง

คุณสมบัติหลัก

ลำดับคุณสมบัติคำอธิบาย
1Countจำนวนคู่คีย์/ค่า
2IsSynchronizedระบุว่า thread-safe หรือไม่
3Item[String]เข้าถึงค่าผ่านคีย์ (indexer)
4Keysคอลเลกชันคีย์ทั้งหมด
5SyncRootออบเจ็กต์สำหรับล็อกการเข้าถึง
6Valuesคอลเลกชันค่าทั้งหมด

เมธอดหลัก

ลำดับเมธอดคำอธิบาย
1Add(String, String)เพิ่มคู่คีย์/ค่า
2Clear()ลบทั้งหมด
3ContainsKey(String)ตรวจสอบคีย์
4ContainsValue(String)ตรวจสอบค่า
5CopyTo(Array, Int32)คัดลอกไปอาร์เรย์

ตัวอย่างที่ 1: ตรวจสอบความเท่ากันของ 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

ตัวอย่างที่ 2: ตรวจสอบ IsSynchronized และวนลูปด้วย IEnumerator

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

สรุป

  • StringDictionary ใช้สำหรับคีย์และค่าที่เป็นสตริงอย่างเดียว
  • ไม่ได้ thread-safe เอง ต้องใช้ lock(strDict.SyncRoot)
  • Equals เปรียบเทียบการอ้างอิง ไม่ใช่เนื้อหา