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#.

Example

 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]);
         }
         Console.ReadKey();
      }
   }  
}

Output

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

Updated on: 23-Jun-2020

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements