C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we will explore the Hashtable
class in C#. A Hashtable
is a non-generic collection class that allows you to store key-value pairs, where each key is unique. The Hashtable
class provides fast lookups and insertions by using a hash table data structure internally.
To use a Hashtable
, you need to add the System.Collections
namespace to your code:
using System.Collections;
You can create a new Hashtable
and add key-value pairs to it:
Hashtable hashtable = new Hashtable(); hashtable.Add("one", 1); hashtable.Add("two", 2); hashtable.Add("three", 3);
You can access the value associated with a key using the indexer:
int value = (int)hashtable["one"]; Console.WriteLine(value); // Output: 1
If the key does not exist in the Hashtable
, the indexer returns null
. You can use the ContainsKey
method to check if a key exists in the Hashtable
:
if (hashtable.ContainsKey("four")) { Console.WriteLine(hashtable["four"]); } else { Console.WriteLine("Key not found."); }
You can update the value associated with a key using the indexer:
hashtable["one"] = 11;
You can remove a key-value pair from the Hashtable
using the Remove
method:
hashtable.Remove("one");
You can use a foreach
loop to enumerate the keys or values in the Hashtable
:
foreach (DictionaryEntry entry in hashtable) { Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}"); }
In this example, we use the DictionaryEntry
structure, which represents a key-value pair in the Hashtable
.
The Hashtable
class has some limitations compared to the generic Dictionary<TKey, TValue>
class:
For these reasons, it is generally recommended to use the generic Dictionary<TKey, TValue>
class instead of the Hashtable
class when working with key-value pairs in C#.
This tutorial demonstrates the basics of using the Hashtable
class in C#. While the Hashtable
class provides fast lookups and insertions, it has some limitations compared to the generic Dictionary<TKey, TValue>
class. In most cases, it is better to use the Dictionary<TKey, TValue>
class for a more type-safe and efficient key-value pair storage.
How to use Hashtable in C#
To use a Hashtable in C#, you need to import the System.Collections
namespace. Here's a simple example:
using System; using System.Collections; class Program { static void Main() { // Creating a Hashtable Hashtable hashtable = new Hashtable(); // Adding key-value pairs hashtable.Add("Key1", "Value1"); hashtable.Add("Key2", "Value2"); // Accessing values Console.WriteLine("Value for Key1: " + hashtable["Key1"]); Console.ReadLine(); } }
Hashtable methods and properties in C#
Hashtable provides various methods and properties. Common methods include Add
, Remove
, ContainsKey
, and properties like Count
. Here's an example:
// Adding more methods and properties to the first example // Removing a key-value pair hashtable.Remove("Key1"); // Checking if a key exists if (hashtable.ContainsKey("Key1")) Console.WriteLine("Key1 is present"); else Console.WriteLine("Key1 is not present"); // Getting the number of key-value pairs Console.WriteLine("Number of pairs: " + hashtable.Count);
Thread safety with Hashtable in C#
Hashtable is not thread-safe. If you need thread safety, consider using Hashtable.Synchronized()
to create a synchronized wrapper:
Hashtable synchronizedHashtable = Hashtable.Synchronized(new Hashtable());
Hashtable iteration in C#
You can iterate through a Hashtable using foreach
:
foreach (DictionaryEntry entry in hashtable) { Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}"); }
Hashtable initialization in C#
You can initialize a Hashtable during declaration:
Hashtable hashtable = new Hashtable() { { "Key1", "Value1" }, { "Key2", "Value2" } };