How to get Synchronize access to the Stack in C# Last Updated : 18 Feb, 2019 Comments Improve Suggest changes Like Article Like Report Stack.SyncRoot Property is used to get an object which can be used to synchronize access to the Stack. Stack represents last-in, first out collection of object. It is used when you need last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. This class comes under System.Collections namespace. Syntax: public virtual object SyncRoot { get; } Property Value: An object which can be used to synchronize access to the Stack. Important Points: Synchronization of an object is done so that only one thread can manipulate the data in the Stack. A property is a member of a class that provides a means for reading, writing and computing private data fields. The synchronizing code cannot directly perform on the collection, so it must perform operations on the SyncRoot of the collection in order to guarantee the proper operation of collections that are derived from other objects. Retrieving the value of this property is an O(1) operation. Below programs illustrate the use of the above-discussed property: Example 1: In this code, we are using SyncRoot to get Synchronized access to the Stack named st, which is not a thread-safe procedure and can cause an exception. So to avoid the exception we lock the collection during the enumeration. csharp // C# program to illustrate the // use of SyncRoot property of // the Stack using System; using System.Threading; using System.Collections; namespace sync_root { class GFG { // Main Method static void Main(string[] args) { // Declaring an Stack Stack st = new Stack(); // Adding elements to Stack st.Push(1); st.Push(2); st.Push(3); st.Push(4); st.Push(5); // Using the SyncRoot property lock(st.SyncRoot) { // foreach loop to display // the elements in st foreach(Object i in st) Console.WriteLine(i); } } } } Output: 5 4 3 2 1 Example 2: csharp // C# program to illustrate the // use of SyncRoot property of // the Stack using System; using System.Threading; using System.Collections; namespace sync_root { class GFG { // Main Method static void Main(string[] args) { // Declaring an Stack Stack st = new Stack(); // Adding elements to Stack st.Push("C"); st.Push("C++"); st.Push("Java"); st.Push("C#"); st.Push("HTML"); // Using the SyncRoot property lock(st.SyncRoot) { // foreach loop to display // the elements in st foreach(Object i in st) Console.WriteLine(i); } } } } Output: HTML C# Java C++ C Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.stack.syncroot?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article Stack.Clear Method in C# K Kirti_Mangal Follow Improve Article Tags : C# CSharp-Collections-Namespace CSharp-Collections-Stack Similar Reads C# Stack Class In C#, the Stack<T> class represents a Last-in-First-out (LIFO) collection of objects. The stack is the part of the System.Collections.Generic namespace. This class allows us to push elements onto the stack, pop elements from the stack, and peek at the top element without removing it.The capac 5 min read C# | How to create a Stack Stack() constructor is used to initialize a new instance of the Stack class which will be empty and will have the default initial capacity. Stack represents a last-in, first out collection of object. It is used when you need last-in, first-out access to items. When you add an item in the list, it is 2 min read Stack.Count Property in C# This method(comes under System.Collections namespace) is used to get the number of elements contained in the Stack. The capacity is the number of elements that the Stack can store and the count is the number of elements that are actually in the Stack. The capacity is always greater than or equal to 2 min read Stack.IsSynchronized Property in C# This method(comes under System.Collections namespace) is used to get a value indicating whether access to the Stack is synchronized (thread safe) or not. To guarantee the thread safety of the Stack, all operations must be done through the wrapper returned by the Synchronized method. Also, retrieving 2 min read How to get Synchronize access to the Stack in C# Stack.SyncRoot Property is used to get an object which can be used to synchronize access to the Stack. Stack represents last-in, first out collection of object. It is used when you need last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you r 3 min read Stack.Clear Method in C# This method(comes under System.Collections namespace) is used to remove all the objects from the Stack. This method will set the Count of Stack to zero, and references to other objects from elements of the collection are also removed. This method is an O(n) operation, where n is Count. Syntax: publi 2 min read Stack.Clone() Method in C# This method is used to create a shallow copy of the Stack. It just creates a copy of the Stack. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array. Syntax: public virtual object Clone (); Return Value: The method returns an Ob 2 min read Stack.Contains() Method in C# This method(comes under System.Collections namespace) is used to check whether a specified element is present is Stack or not. Internally this method checks for equality by calling the Object.Equals method. Also, it performs a linear search, therefore, this method is an O(n) operation, where n is Co 2 min read Stack.CopyTo() Method in C# This method(comes under System.Collections namespace) is used to copy the Stack to an existing 1-D Array which starts from the specified array index. The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to Po 2 min read Stack.Equals() Method in C# Equals(Object) Method which is inherited from the Object class is used to check if a specified Stack class object is equal to another Stack class object or not. This method comes under the System.Collections namespace. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is 2 min read Like