C# Hashtable SyncRoot

Description

Hashtable SyncRoot gets an object that can be used to synchronize access to the Hashtable.

Syntax

Hashtable.SyncRoot has the following syntax.


public virtual Object SyncRoot { get; }

Example

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


using System.Collections;
using System;//from  w w w . j  a v  a 2 s  .com
public class MainClass{
  public static void Main(String[] argv){  
    Hashtable myCollection = new Hashtable();
    lock(myCollection.SyncRoot)
    {
        foreach (object item in myCollection)
        {
            // Insert your code here.
        }
    }
  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack