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

การตรวจสอบว่า Dictionary มีคีย์ที่ระบุหรือไม่ใน C#

ภาพรวม

Dictionary<TKey, TValue>.ContainsKey(TKey key) ตรวจสอบว่ามีคีย์ใน Dictionary หรือไม่ คืนค่า bool ใช้ hashing O(1) เร็วและปลอดภัยแบบเจนเนริก

ตัวอย่างที่ 1: ตรวจสอบคีย์ที่มีอยู่ ("Three")

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("One", "John");
        dict.Add("Two", "Tom");
        dict.Add("Three", "Jacob");
        dict.Add("Four", "Kevin");
        dict.Add("Five", "Nathan");

        Console.WriteLine("Count of elements = " + dict.Count);
        Console.WriteLine("\nKey/value pairs...");
        foreach (KeyValuePair<string, string> res in dict)
        {
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }

        if (dict.ContainsKey("Three"))
            Console.WriteLine("Key found!");
        else
            Console.WriteLine("Key isn't in the dictionary!");

        dict.Clear();
        Console.WriteLine("Cleared Key/value pairs...");
        foreach (KeyValuePair<string, string> res in dict)
        {
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }
        Console.WriteLine("Count of elements now = " + dict.Count);
    }
}

ผลลัพธ์

Count of elements = 5
Key/value pairs...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
Key found!
Cleared Key/value pairs...
Count of elements now = 0

ตัวอย่างที่ 2: ตรวจสอบคีย์ที่ไม่มีอยู่ ("mykey")

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("One", "John");
        dict.Add("Two", "Tom");
        dict.Add("Three", "Jacob");
        dict.Add("Four", "Kevin");
        dict.Add("Five", "Nathan");

        Console.WriteLine("Count of elements = " + dict.Count);
        Console.WriteLine("\nKey/value pairs...");
        foreach (KeyValuePair<string, string> res in dict)
        {
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }

        if (dict.ContainsKey("mykey"))
            Console.WriteLine("Key found!");
        else
            Console.WriteLine("Key isn't in the dictionary!");

        dict.Clear();
        Console.WriteLine("Cleared Key/value pairs...");
        foreach (KeyValuePair<string, string> res in dict)
        {
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }
        Console.WriteLine("Count of elements now = " + dict.Count);
    }
}

ผลลัพธ์

Count of elements = 5
Key/value pairs...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
Key isn't in the dictionary!
Cleared Key/value pairs...
Count of elements now = 0

สรุป

  • ContainsKey() ใช้ตรวจสอบคีย์ก่อนเพิ่ม/เข้าถึง ป้องกัน Exception
  • Dictionary<TKey,TValue> เป็น generic type-safe เร็วกว่า Hashtable
  • Clear() ล้างข้อมูลทั้งหมด Count = 0
  • ใช้ TryGetValue() ถ้าต้องการค่าพร้อมกัน (ประหยัดการ lookup 2 ครั้ง)