How to Get Value from HashTable Collection in C# using Specified Key

A Hashtable is a collection of key-value pairs that provides fast lookup by key. In C#, you can retrieve values from a hashtable using the indexer syntax hashtable[key] or by checking if a key exists using the Contains() method. This is essential for accessing specific data when you know the corresponding key.

Syntax

Following is the syntax for getting a value from a hashtable using a specified key

// Check if key exists
if (hashtable.Contains(key)) {
   object value = hashtable[key];
}

// Direct access (may return null if key doesn't exist)
object value = hashtable[key];

How It Works

When retrieving a value from a hashtable, follow these steps

  • Use the Contains() method to check if the key exists

  • Access the value using the indexer hashtable[key]

  • Cast the returned object to the appropriate type if needed

  • Handle cases where the key doesn't exist

Hashtable Key-Value Lookup "UK" "US" "FR" "London" "New York" "Paris" Keys Values hashtable["UK"] returns "London"

Using Contains() Method to Check Key Existence

Example

using System;
using System.Collections;

class Program {
   static void Main() {
      // Create a hashtable instance
      Hashtable cityTable = new Hashtable();

      // Adding key/value pairs
      cityTable.Add("US", "New York");
      cityTable.Add("FR", "Paris");
      cityTable.Add("UK", "London");
      cityTable.Add("IN", "Mumbai");
      cityTable.Add("GER", "Berlin");

      string key = "UK";
      
      if (cityTable.Contains(key)) {
         string value = (string)cityTable[key];
         Console.WriteLine("The value of key {0} = {1}", key, value);
      } else {
         Console.WriteLine("Key {0} does not exist", key);
      }

      // Try with non-existing key
      key = "CAN";
      if (cityTable.Contains(key)) {
         string value = (string)cityTable[key];
         Console.WriteLine("The value of key {0} = {1}", key, value);
      } else {
         Console.WriteLine("Key {0} does not exist", key);
      }
   }
}

The output of the above code is

The value of key UK = London
Key CAN does not exist

Direct Value Access with Null Check

Example

using System;
using System.Collections;

class Program {
   static void Main() {
      Hashtable numberNames = new Hashtable();

      // Adding key/value pairs
      numberNames.Add("1.1", "One point One");
      numberNames.Add("1.2", "One point Two");
      numberNames.Add("1.3", "One point Three");
      numberNames.Add("1.4", "One point Four");
      numberNames.Add("1.5", "One point Five");

      string key = "1.4";
      object value = numberNames[key];
      
      if (value != null) {
         Console.WriteLine("The value of key {0} = {1}", key, value);
      } else {
         Console.WriteLine("Key {0} does not exist or has null value", key);
      }

      // Test with non-existing key
      key = "2.0";
      value = numberNames[key];
      
      if (value != null) {
         Console.WriteLine("The value of key {0} = {1}", key, value);
      } else {
         Console.WriteLine("Key {0} does not exist or has null value", key);
      }
   }
}

The output of the above code is

The value of key 1.4 = One point Four
Key 2.0 does not exist or has null value

Safe Value Retrieval with Type Casting

Example

using System;
using System.Collections;

class Program {
   static void Main() {
      Hashtable mixedTable = new Hashtable();

      // Adding different types of values
      mixedTable.Add("name", "John");
      mixedTable.Add("age", 25);
      mixedTable.Add("salary", 50000.50);

      // Safe string retrieval
      if (mixedTable.Contains("name")) {
         string name = mixedTable["name"] as string;
         Console.WriteLine("Name: " + name);
      }

      // Safe integer retrieval
      if (mixedTable.Contains("age")) {
         int age = (int)mixedTable["age"];
         Console.WriteLine("Age: " + age);
      }

      // Safe double retrieval
      if (mixedTable.Contains("salary")) {
         double salary = (double)mixedTable["salary"];
         Console.WriteLine("Salary: $" + salary);
      }
   }
}

The output of the above code is

Name: John
Age: 25
Salary: $50000.5

Comparison of Access Methods

Method Advantage Disadvantage
Contains() + indexer Safe, no exceptions thrown Two operations required
Direct indexer access Single operation, faster Returns null if key doesn't exist
TryGetValue() equivalent Not available in Hashtable Use Dictionary<T,T> instead

Conclusion

Getting values from a Hashtable using specified keys involves checking key existence with Contains() method and accessing values with the indexer syntax. Always handle cases where keys might not exist to avoid null reference issues and ensure robust code execution.

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

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements