Hashtable ht = new Hashtable(); ht.Add("name", "John"); ht.Add("age", 30); if(ht.ContainsKey("name")) { //do something }
using System.Collections; class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add(1, "one"); ht.Add(2, "two"); ht.Add(3, "three"); ht.Add(4, "four"); bool keyExists = ht.ContainsKey(3); Console.WriteLine(keyExists); // output: True keyExists = ht.ContainsKey(5); Console.WriteLine(keyExists); // output: False } }In this example, a hashtable is created and four key/value pairs are added. The ContainsKey method is used to check if the hashtable contains the key 3 and then the key 5. The package library for the System.Collections Hashtable class is part of the .NET Framework Class Library.