Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Kiểm tra StringDictionary có chứa khóa cụ thể trong C#

Giới thiệu

Để kiểm tra xem StringDictionary có chứa một khóa cụ thể hay không, ta sử dụng phương thức ContainsKey(). Phương thức này trả về true nếu khóa tồn tại, ngược lại trả về false.

Ví dụ 1

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("Các phần tử StringDictionary1...");
        foreach (DictionaryEntry d in strDict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("StringDictionary1 có khóa G? " + strDict1.ContainsKey("G"));

        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("\nCác phần tử StringDictionary2...");
        foreach (DictionaryEntry d in strDict2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("\nDictionary2 có bằng Dictionary1? = " + strDict2.Equals(strDict1));
    }
}

Kết quả

Các phần tử StringDictionary1...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary1 có khóa G? True

Các phần tử StringDictionary2...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Dictionary2 có bằng Dictionary1? = False

Ví dụ 2

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("Các phần tử StringDictionary1...");
        foreach (DictionaryEntry d in strDict1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("StringDictionary1 có khóa G? " + strDict1.ContainsKey("G"));

        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("\nCác phần tử StringDictionary2...");
        foreach (DictionaryEntry d in strDict2)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }

        Console.WriteLine("\nDictionary2 có bằng Dictionary1? = " + strDict2.Equals(strDict1));
        Console.WriteLine("StringDictionary2 có khóa B? " + strDict2.ContainsKey("B"));
    }
}

Kết quả

Các phần tử StringDictionary1...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary1 có khóa G? True

Các phần tử StringDictionary2...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Dictionary2 có bằng Dictionary1? = False
StringDictionary2 có khóa B? True

Lưu ý quan trọng

  • Phương thức ContainsKey() có độ phức tạp O(1) trung bình, nhanh hơn ContainsValue().
  • StringDictionary tự động chuyển khóa thành chữ thường, do đó việc tìm kiếm khóa không phân biệt chữ hoa/chữ thường.
  • Phương thức Equals() so sánh tham chiếu đối tượng, không so sánh nội dung. Hai dictionary có cùng nội dung nhưng là instance khác nhau sẽ trả về false.
  • Trong .NET hiện đại, khuyến nghị sử dụng Dictionary<string, string> thay vì StringDictionary.