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

In C#, you can get or set the value associated with a specified key in a Hashtable using the indexer hashtable[key]. This provides a convenient way to access and modify values using their keys.

Syntax

Following is the syntax for getting a value from a Hashtable −

object value = hashtable[key];

Following is the syntax for setting a value in a Hashtable −

hashtable[key] = value;

Getting Values from Hashtable

You can retrieve values from a Hashtable using the key as an index. If the key doesn't exist, it returns null

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");
      
      Console.WriteLine("Value at key 3 = " + hash["3"]);
      Console.WriteLine("Value at key 1 = " + hash["1"]);
      Console.WriteLine("Value at non-existent key = " + hash["99"]);
   }
}

The output of the above code is −

Value at key 3 = DE
Value at key 1 = AB
Value at non-existent key = 

Setting Values in Hashtable

You can modify existing values or add new key-value pairs using the indexer. If the key exists, it updates the value; if not, it adds a new entry −

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");
      
      Console.WriteLine("Original value at key 3 = " + hash["3"]);
      
      // Update existing key
      hash["3"] = "XY";
      Console.WriteLine("Updated value at key 3 = " + hash["3"]);
      
      // Add new key-value pair
      hash["4"] = "PQ";
      Console.WriteLine("New value at key 4 = " + hash["4"]);
      
      Console.WriteLine("Total entries: " + hash.Count);
   }
}

The output of the above code is −

Original value at key 3 = DE
Updated value at key 3 = XY
New value at key 4 = PQ
Total entries: 4

Key Differences: Add() vs Indexer

Method Behavior When Key Exists
Add(key, value) Adds new key-value pair Throws ArgumentException
hashtable[key] = value Adds or updates key-value pair Updates the existing value
hashtable[key] Gets value by key Returns the value or null

Practical Example with Error Handling

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Hashtable studentGrades = new Hashtable();
      
      // Add initial grades
      studentGrades["Alice"] = 85;
      studentGrades["Bob"] = 92;
      studentGrades["Charlie"] = 78;
      
      // Get and display grades
      Console.WriteLine("Alice's grade: " + studentGrades["Alice"]);
      Console.WriteLine("Bob's grade: " + studentGrades["Bob"]);
      
      // Update a grade
      studentGrades["Alice"] = 90;
      Console.WriteLine("Alice's updated grade: " + studentGrades["Alice"]);
      
      // Check for non-existent student
      object grade = studentGrades["David"];
      if (grade == null) {
         Console.WriteLine("David not found in records");
      } else {
         Console.WriteLine("David's grade: " + grade);
      }
   }
}

The output of the above code is −

Alice's grade: 85
Bob's grade: 92
Alice's updated grade: 90
David not found in records

Conclusion

The Hashtable indexer hashtable[key] provides an efficient way to get and set values by key. Unlike the Add() method, the indexer can both add new entries and update existing ones without throwing exceptions, making it more flexible for dynamic operations.

Updated on: 2026-03-17T07:04:36+05:30

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements