Hashtable.Contains() (tương đương ContainsKey()) kiểm tra khóa có tồn tại trong Hashtable 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, "Jack");
h.Add(2, "Henry");
h.Add(3, "Ben");
h.Add(4, "Chris");
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.Contains(3));
}
}
Kết quả đầu ra
Danh sách Key và Value: Key = 4, Value = Chris Key = 3, Value = Ben Key = 2, Value = Henry Key = 1, Value = Jack Key 3 tồn tại? True
Khuyến nghị: Dùng ContainsKey() thay vì Contains() cho rõ ý đồ (Contains() là method cũ từ IDictionary).