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

ตรวจสอบว่า StringDictionary มีค่าเฉพาะหรือไม่ใน C#

บทความนี้สาธิตวิธีตรวจสอบว่า StringDictionary มีค่า (value) ที่ระบุอยู่หรือไม่ โดยใช้เมธอด ContainsValue() ใน C#

ตัวอย่างที่ 1: ตรวจสอบค่าและคีย์ใน 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 d in strDict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("Does StringDictionary1 has key G? " + strDict1.ContainsKey("G"));
        Console.WriteLine("Does StringDictionary1 has value Ryan? " + strDict1.ContainsValue("Ryan"));

        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("\nIs Dictionary2 equal to Dictionary1? = " + strDict2.Equals(strDict1));
        Console.WriteLine("Does StringDictionary2 has key B? " + strDict2.ContainsKey("B"));
    }
}

ผลลัพธ์

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Does StringDictionary1 has key G? True
Does StringDictionary1 has value Ryan? True
StringDictionary2 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Is Dictionary2 equal to Dictionary1? = False
Does StringDictionary2 has key B? True

ตัวอย่างที่ 2: ตรวจสอบค่าที่ไม่มีอยู่ใน StringDictionary

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 has key G? " + strDict1.ContainsKey("G"));
        Console.WriteLine("Does StringDictionary1 has value Footwear? " + strDict1.ContainsValue("Footwear"));

        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("\nIs Dictionary2 equal to Dictionary1? = " + strDict2.Equals(strDict1));
        Console.WriteLine("Does StringDictionary2 has key B? " + strDict2.ContainsKey("B"));
    }
}

ผลลัพธ์

StringDictionary1 elements...
x Accessories
u Electronics
v Toys
w Books
Does StringDictionary1 has key G? False
Does StringDictionary1 has value Footwear? False
StringDictionary2 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Is Dictionary2 equal to Dictionary1? = False
Does StringDictionary2 has key B? True

สรุป

  • ใช้ ContainsValue() เพื่อตรวจสอบว่ามีค่าที่ระบุอยู่ใน StringDictionary หรือไม่
  • ใช้ ContainsKey() เพื่อตรวจสอบคีย์
  • เมธอด Equals() เปรียบเทียบอ้างอิงออบเจ็กต์ ไม่ใช่เนื้อหา ดังนั้นจึงคืนค่า False แม้ข้อมูลจะเหมือนกัน