C# Stack Synchronized

Description

Stack Synchronized returns a synchronized (thread safe) wrapper for the Stack.

Syntax

Stack.Synchronized has the following syntax.


[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true)]
public static Stack Synchronized(
  Stack stack
)

Parameters

Stack.Synchronized has the following parameters.

  • stack - The Stack to synchronize.

Returns

Stack.Synchronized method returns A synchronized wrapper around the Stack.

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.jav  a 2 s .  c  om*/
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