Get or Set the value associated with specified key in Hashtable in C#

CsharpServer Side ProgrammingProgramming

To get or set the value associated with specified key in Hashtable, the code is as follows −

Example

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("1", "AB");
      hash.Add("2", "BC");
      hash.Add("3", "DE");
      hash.Add("4", "EF");
      hash.Add("5", "GH");
      hash.Add("6", "IJ");
      hash.Add("7", "KL");
      hash.Add("8", "MN");
      hash.Add("9", "OP");
      hash.Add("10", "QR");
      Console.WriteLine("Value at key 3 = "+hash["3"]);
   }
}

Output

This will produce the following output −

Value at key 3 = DE

Example

Let us see another example −

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("1", "AB");
      hash.Add("2", "BC");
      hash.Add("3", "DE");
      hash.Add("4", "EF");
      hash.Add("5", "GH");
      hash.Add("6", "IJ");
      hash.Add("7", "KL");
      hash.Add("8", "MN");
      hash.Add("9", "OP");
      hash.Add("10", "QR");
      Console.WriteLine("Value at key 3 = "+hash["3"]);
      hash["3"] = "UV";
      Console.WriteLine("Value at key 3 (UPDATED) = "+hash["3"]);
   }
}

Output

This will produce the following output −

Value at key 3 = DE
Value at key 3 (UPDATED) = UV
raja
Published on 10-Dec-2019 11:02:19
Advertisements