Hashtable.ContainsKey() kiểm tra xem khóa chỉ định có tồn tại trong Hashtable hay không. Trả về true nếu tìm thấy, false nếu không.
Ví dụ minh họa
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
Hashtable h = new Hashtable();
h.Add(1, "Sam");
h.Add(2, "Jack");
h.Add(3, "Andy");
h.Add(4, "Katie");
h.Add(5, "Beth");
h.Add(6, "Benjamin");
Console.WriteLine("Danh sách Key và Value:");
foreach (var key in h.Keys)
Console.WriteLine("Key = {0}, Value = {1}", key, h[key]);
Console.WriteLine("Key 3 tồn tại? " + h.ContainsKey(3));
}
}
Kết quả đầu ra
Danh sách Key và Value: Key = 6, Value = Benjamin Key = 5, Value = Beth Key = 4, Value = Katie Key = 3, Value = Andy Key = 2, Value = Jack Key = 1, Value = Sam Key 3 tồn tại? True
Phương thức liên quan: ContainsValue() (kiểm tra giá trị), Contains() (alias của ContainsKey).