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

วิธีดึงจำนวนคู่คีย์/ค่าใน StringDictionary ใน C#

บทความนี้สาธิตวิธีการดึงจำนวนคู่คีย์/ค่า (key/value pairs) ใน StringDictionary โดยใช้ property Count

ตัวอย่างที่ 1: นับจำนวนคู่คีย์/ค่า

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo
{
    public static void Main()
    {
        StringDictionary strDict = new StringDictionary();
        strDict.Add("1", "One");
        strDict.Add("2", "Two");
        strDict.Add("3", "Three");
        strDict.Add("4", "Four");

        Console.WriteLine("StringDictionary key-value pairs...");
        foreach (DictionaryEntry d in strDict)
        {
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }

        Console.WriteLine("Count of StringDictionary key-value pairs... " + strDict.Count);
    }
}

ผลลัพธ์

StringDictionary key-value pairs...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Count of StringDictionary key-value pairs... 4

ตัวอย่างที่ 2: ตัวอย่างเพิ่มเติมกับหลาย Dictionary

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);
        }
        Console.WriteLine("Count of StringDictionary1 key-value pairs... " + strDict1.Count);

        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...");
        foreach (DictionaryEntry de in strDict2)
        {
            Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
        }
        Console.WriteLine("Count of StringDictionary2 key-value pairs... " + strDict2.Count);
    }
}

ผลลัพธ์

StringDictionary1 elements...
A John
B Andy
C Tim
D Ryan
E Kevin
F Katie
G Brad
Count of StringDictionary1 key-value pairs... 7

StringDictionary2 key-value pairs...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E
Count of StringDictionary2 key-value pairs... 5

คำอธิบายเพิ่มเติม

  • StringDictionary เป็น dictionary เฉพาะสำหรับ string key และ string value
  • Property Count คืนจำนวนคู่คีย์/ค่าเป็น integer
  • ไม่ thread-safe - ต้องใช้ lock เองถ้าเข้าถึงจากหลายเธรด
  • Key ไม่สนใจตัวพิมพ์ใหญ่เล็ก (case-insensitive) - "A" และ "a" ถือว่าเหมือนกัน
  • ใช้ foreach กับ DictionaryEntry เพื่อวนลูปอ่านคีย์และค่าได้พร้อมกัน