C# Stack SyncRoot

Description

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

Syntax

Stack.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;//  w w w.ja v  a 2s.com
public class MainClass{
  public static void Main(String[] argv){  
    Stack myCollection = new Stack();
    
    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