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 Create a HashTable Collection in C#?
The Hashtable is a non-generic collection in C# that stores key-value pairs, similar to a generic Dictionary collection. Hashtable is defined in the System.Collections namespace and computes hash codes for keys to optimize lookups by storing them in different internal buckets.
In this tutorial, we will see how to create a Hashtable collection in C# using different approaches.
Key Features of Hashtable
Stores key-value pairs as
DictionaryEntryobjectsKeys must be unique and non-null, but values can be null or duplicate
Keys are immutable objects that provide hash functions
Values can be accessed using keys in an indexer like arrays
Elements can be of the same or different types
Implements
IDictionary,ICollection,IEnumerable, and other interfaces
Syntax
Following is the syntax for creating a Hashtable
using System.Collections; Hashtable hashtableName = new Hashtable();
Adding elements using the Add() method
hashtableName.Add(key, value);
Common Hashtable Constructors
| Constructor | Description |
|---|---|
Hashtable() |
Creates an empty Hashtable with default capacity and load factor |
Hashtable(IDictionary) |
Creates a Hashtable initialized with contents from specified dictionary |
Hashtable(int) |
Creates an empty Hashtable with specified initial capacity |
Using Default Constructor
Example
using System;
using System.Collections;
class MyHashTable {
static public void Main() {
// Create hashtable using the default constructor
Hashtable indianNumberSystem = new Hashtable();
// Add key/value pairs using the Add() method
indianNumberSystem.Add(1, "Ones");
indianNumberSystem.Add(10, "Tens");
indianNumberSystem.Add(100, "Hundred");
indianNumberSystem.Add(1000, "Thousand");
indianNumberSystem.Add(10000, "Ten Thousand");
indianNumberSystem.Add(100000, "Lac");
indianNumberSystem.Add(1000000, "Ten Lac");
indianNumberSystem.Add(10000000, "Crore");
// Display HashTable contents
Console.WriteLine("Key-Value pairs from Indian Number System:");
foreach(DictionaryEntry element in indianNumberSystem) {
Console.WriteLine("{0} : {1}", element.Key, element.Value);
}
}
}
The output of the above code is
Key-Value pairs from Indian Number System: 100 : Hundred 1000 : Thousand 10 : Tens 1000000 : Ten Lac 100000 : Lac 10000000 : Crore 10000 : Ten Thousand 1 : Ones
Using Collection Initializer
Example
using System;
using System.Collections;
class MyHashTable {
static public void Main() {
// Create hashtable with collection initializer
Hashtable cityHashtable = new Hashtable() {
{"USA", "New York"},
{"France", "Paris"},
{"UK", "London"},
{"India", "Mumbai"},
{"Germany", "Berlin"}
};
Console.WriteLine("Countries and Cities:");
foreach(DictionaryEntry element in cityHashtable) {
Console.WriteLine("{0} : {1}", element.Key, element.Value);
}
}
}
The output of the above code is
Countries and Cities: Germany : Berlin France : Paris USA : New York UK : London India : Mumbai
Creating from Existing Dictionary
Example
using System;
using System.Collections;
class MyHashTable {
static public void Main() {
// Create initial hashtable
Hashtable originalTable = new Hashtable() {
{"C#", "Programming Language"},
{"HTML", "Markup Language"}
};
// Create new hashtable from existing one
Hashtable copiedTable = new Hashtable(originalTable);
copiedTable.Add("SQL", "Database Language");
Console.WriteLine("Copied and Extended Hashtable:");
foreach(DictionaryEntry element in copiedTable) {
Console.WriteLine("{0} : {1}", element.Key, element.Value);
}
}
}
The output of the above code is
Copied and Extended Hashtable: HTML : Markup Language C# : Programming Language SQL : Database Language
Conclusion
Hashtable in C# provides an efficient way to store key-value pairs with fast lookup capabilities through hashing. While it's a non-generic collection, it remains useful for scenarios requiring mixed data types or when working with legacy code that doesn't use generics.
