System.Collections.Hashtable.Synchronized Method

Returns a synchronized (thread-safe) wrapper for the Hashtable.

Syntax

public static Hashtable Synchronized (Hashtable table)

Parameters

table
The Hashtable to synchronize.

Returns

A synchronized (thread-safe) wrapper for the Hashtable.

Exceptions

TypeReason
ArgumentNullException table is null.

Remarks

The Hashtable.Synchronized(Hashtable) method is thread safe for multiple readers and writers. Furthermore, the synchronized wrapper ensures that there is only one writer writing at a time.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

The following code example shows how to lock the collection using the Hashtable.SyncRoot during the entire enumeration:

code reference: Classic Hashtable.IsSynchronized Example#2

This method is an O(1) operation.

Requirements

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0