C# Hashtable

C# Hashtable

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.

  • Creating a Hashtable

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); 
  • Accessing Values by Key

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."); } 
  • Updating and Removing Values

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"); 
  • Enumerating the Hashtable

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.

  • Limitations of Hashtable

The Hashtable class has some limitations compared to the generic Dictionary<TKey, TValue> class:

  • It is not type-safe, meaning that you need to cast the keys and values to their correct types.
  • It does not provide good performance with value types, as they need to be boxed and unboxed when stored and retrieved.
  • It is not guaranteed to maintain the order of the keys and values.

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.

Examples

  1. 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(); } } 
  2. 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); 
  3. 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()); 
  4. 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}"); } 
  5. Hashtable initialization in C#

    You can initialize a Hashtable during declaration:

    Hashtable hashtable = new Hashtable() { { "Key1", "Value1" }, { "Key2", "Value2" } }; 

More Tags

google-signin parent playframework updating xml-serialization compose-db pysftp xslt-grouping pydot findall

More Programming Guides

Other Guides

More Programming Examples