07 Feb 2024




Intermediate

HashTable collections in C# are a type of data structure that store key-value pairs. Each key is unique and is used to efficiently retrieve its corresponding value. Think of it like a phone book, where each person's name (key) helps you find their phone number (value).

Here are some key points about HashTable collections:

  • Storage based on hash codes: Keys are converted to a unique number called a "hash code" using a built-in hash function. This hash code determines where the key-value pair is stored within the collection.
  • Fast lookups: By using the hash code, the HashTable can quickly locate the desired key-value pair, making lookups much faster than searching through a linear list.
  • Non-generic: Unlike modern collections like Dictionary, HashTable doesn't enforce specific data types for keys and values. They can be of any type as long as the key implements the GetHashCode method for proper hashing.
  • Not thread-safe: Using a HashTable concurrently from multiple threads without proper synchronization can lead to data inconsistencies.
  • Limited functionality: Compared to Dictionary, HashTable offers fewer features like sorting or ordering elements.

When to use HashTable:

  • When fast lookups by key are essential.
  • When dealing with collections of mixed data types.
  • For legacy code that was written before generics were introduced (C# 2.0).

Alternatives to HashTable:

  • Dictionary: The generally preferred choice in modern C# due to its generic nature, thread-safety, and richer functionality.
  • ConcurrentDictionary: For thread-safe operations in multithreaded environments.

💡Remember: While HashTable still exists in C#, it's generally recommended to use Dictionary for most scenarios due to its advantages.

Here's a basic example of using a HashTable in C#:

using System;
using System.Collections;

class Program
{
    static void Main(string[] args)
    {
        // Creating a new HashTable instance
        Hashtable hashtable = new Hashtable();

        // Adding key-value pairs
        hashtable.Add("John", 25);
        hashtable.Add("Alice", 30);
        hashtable.Add("Bob", 35);

        // Retrieving values by keys
        Console.WriteLine("John's age: " + hashtable["John"]);
        Console.WriteLine("Alice's age: " + hashtable["Alice"]);
        Console.WriteLine("Bob's age: " + hashtable["Bob"]);

        // Checking if a key exists
        if (hashtable.ContainsKey("Alice"))
        {
            Console.WriteLine("Alice's age is present in the Hashtable.");
        }

        // Removing a key-value pair
        hashtable.Remove("Bob");

        // Iterating through key-value pairs
        foreach (DictionaryEntry entry in hashtable)
        {
            Console.WriteLine("Key: " + entry.Key + ", Value: " + entry.Value);
        }
    }
}

In this example, we create a HashTable, add key-value pairs, retrieve values by keys, check for key existence, remove a key-value pair, and iterate through the elements using a foreach loop.

c-sharp
hashtable
collections