What is the Item property of Hashtable class in C#?

Gets or sets the value associated with the specified key. You can also use the Item property to add new elements.

If a key does not exist, then you can include it like −

myCollection["myNonexistentKey"] = myValue

The following is the code showing how to work with Item property of Hashtable class in C#.


 Live Demo

using System;
using System.Collections;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         Hashtable ht = new Hashtable();
         ht.Add("One", "Amit");
         ht.Add("Two", "Aman");
         ht.Add("Three", "Raman");
         ht["Four"] = "David";
         ICollection key = ht.Keys;
         foreach (string k in key) {
            Console.WriteLine(k + ": " + ht[k]);


Three: Raman
Four: David
One: Amit
Two: Aman