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

CsharpProgrammingServer Side Programming

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
raja
Published on 17-Sep-2018 14:08:01
Advertisements