Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get keys from a HashTable in C#?
The Hashtable is a non-generic collection in C# that stores key-value pairs, similar to the generic Dictionary collection. It is defined in the System.Collections namespace.
A Hashtable consists of key/value pairs where each key is computed as a hash code and stored in different buckets internally. When accessing the Hashtable, this hash code is matched to locate the corresponding value, which optimizes lookup performance.
Let's explore how to retrieve keys from a Hashtable in C#.
Syntax
Following is the syntax for accessing Hashtable keys using a foreach loop
foreach(DictionaryEntry entry in hashtable) {
Console.WriteLine(entry.Key);
}
Following is the syntax for getting keys using the Keys collection
ICollection keys = hashtable.Keys;
foreach(string key in keys) {
Console.WriteLine(key);
}
Using Foreach Loop with DictionaryEntry
In this approach, we iterate through the Hashtable using a foreach loop with DictionaryEntry to access both keys and values
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");
// Print hashtable keys only
Console.WriteLine("City Table Keys:");
foreach(DictionaryEntry entry in cityTable) {
Console.WriteLine(entry.Key);
}
}
}
The output of the above code is
City Table Keys: FR US IN GER UK
Displaying Both Keys and Values
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");
// Print hashtable key-value pairs
Console.WriteLine("City Table Key-Value Pairs:");
foreach(DictionaryEntry entry in cityTable) {
Console.WriteLine("{0} = {1}", entry.Key, entry.Value);
}
}
}
The output of the above code is
City Table Key-Value Pairs: FR = Paris US = New York IN = Mumbai GER = Berlin UK = London
Using Keys Collection
In this approach, we first retrieve the collection of keys using the Keys property and then iterate through this collection
using System;
using System.Collections;
class Program {
static void Main() {
// Create a hashtable instance
Hashtable langTable = new Hashtable();
// Adding key/value pairs
langTable.Add("CPP", "C++");
langTable.Add("CS", "C#");
langTable.Add("JAVA", "Java");
langTable.Add("PL", "Perl");
langTable.Add("JS", "JavaScript");
// Get collection of keys
ICollection keys = langTable.Keys;
Console.WriteLine("Language Keys:");
foreach(string key in keys) {
Console.WriteLine(key);
}
}
}
The output of the above code is
Language Keys: PL JS CS JAVA CPP
Comparison of Approaches
| Foreach with DictionaryEntry | Keys Collection |
|---|---|
| Access both keys and values in one iteration | Access only keys, more focused approach |
| Uses DictionaryEntry type for iteration | Uses ICollection interface for keys |
| More common for general hashtable operations | Better when you only need keys |
Conclusion
You can retrieve keys from a Hashtable in C# using either a foreach loop with DictionaryEntry or by accessing the Keys collection. The foreach approach is more versatile as it provides access to both keys and values, while the Keys collection approach is more efficient when you only need the keys.
