Creating a synchronized wrapper for the Hashtable in C#

A synchronized wrapper for the Hashtable in C# ensures thread-safe operations when multiple threads access the same Hashtable simultaneously. By default, Hashtable collections are not synchronized, but you can create a thread-safe version using the Hashtable.Synchronized() method.

Syntax

Following is the syntax for creating a synchronized Hashtable wrapper −

Hashtable synchronizedHashtable = Hashtable.Synchronized(originalHashtable);

To check if a Hashtable is synchronized, use the IsSynchronized property −

bool isSynchronized = hashtable.IsSynchronized;

Understanding Thread Safety

Hashtable Thread Safety Regular Hashtable IsSynchronized = False Not thread-safe Multiple threads can cause data corruption Synchronized Hashtable IsSynchronized = True Thread-safe Multiple threads can safely access data Synchronized()

Checking Synchronization Status

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("1", "AB");
      hash.Add("2", "CD");
      hash.Add("3", "EF");
      hash.Add("4", "GH");
      hash.Add("5", "IJ");
      hash.Add("6", "KL");
      
      Console.WriteLine("Hashtable elements...");
      foreach(DictionaryEntry d in hash) {
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("Hashtable is synchronized? = " + hash.IsSynchronized);
   }
}

The output of the above code is −

Hashtable elements...
Key = 1, Value = AB
Key = 2, Value = CD
Key = 3, Value = EF
Key = 4, Value = GH
Key = 5, Value = IJ
Key = 6, Value = KL
Hashtable is synchronized? = False

Creating a Synchronized Wrapper

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("1", "Mark");
      hash.Add("2", "Gary");
      hash.Add("3", "Jacob");
      hash.Add("4", "Andy");
      hash.Add("5", "Jack");
      
      Console.WriteLine("Hashtable elements...");
      foreach(DictionaryEntry d in hash) {
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }
      
      Console.WriteLine("Original Hashtable is synchronized? = " + hash.IsSynchronized);
      
      Hashtable hash2 = Hashtable.Synchronized(hash);
      Console.WriteLine("Synchronized wrapper is synchronized? = " + hash2.IsSynchronized);
   }
}

The output of the above code is −

Hashtable elements...
Key = 1, Value = Mark
Key = 2, Value = Gary
Key = 3, Value = Jacob
Key = 4, Value = Andy
Key = 5, Value = Jack
Original Hashtable is synchronized? = False
Synchronized wrapper is synchronized? = True

Key Points

  • The Hashtable.Synchronized() method returns a wrapper around the original Hashtable, not a new copy.

  • Changes made to the synchronized wrapper affect the original Hashtable and vice versa.

  • The synchronized wrapper provides thread-safe access for concurrent read and write operations.

  • Use synchronized wrappers when multiple threads need to access the same Hashtable simultaneously.

Conclusion

The Hashtable.Synchronized() method creates a thread-safe wrapper around an existing Hashtable, enabling safe concurrent access from multiple threads. This approach maintains data integrity in multi-threaded applications while preserving the original Hashtable's functionality.

Updated on: 2026-03-17T07:04:36+05:30

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements