Hashtable fruits = new Hashtable(); // Creating an instance of Hashtable fruits.Add("Apple", 50); // Adding key-value pair fruits.Add("Orange", 30); fruits.Add("Mango", 70); Console.WriteLine(fruits["Apple"]); // Accessing value using key
Hashtable person = new Hashtable() { { "Name", "John" }, // Initializing Hashtable while creating instance { "Age", 25 } }; Console.WriteLine(person["Age"]); // Accessing value using keyThis example shows how to create a Hashtable with initial values using an object initializer. It also demonstrates how to access a value associated with a key. Overall, the Hashtable is a useful collection type when dealing with key-value pairs.