Open In App

Stack.IsSynchronized Property in C#

Improve
Improve
Like Article
Like
Save
Share
Report

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 the value of this property is an O(1) operation.

Syntax:

public virtual bool IsSynchronized { get; }

Return Value: This property returns true, if access to the Stack is synchronized (thread safe) otherwise it returns false. The default is false.

Below programs illustrate the use of above-discussed property:

Example 1:




// C# code to illustrate the
// Stack.IsSynchronized Property
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Stack
        Stack myStack = new Stack();
  
        // Inserting the elements into the Stack
        myStack.Push("Geeks");
        myStack.Push("Geeks Classes");
        myStack.Push("Noida");
        myStack.Push("Data Structures");
        myStack.Push("GeeksforGeeks");
  
        // Creates a synchronized
        // wrapper around the Stack
        Stack ss = Stack.Synchronized(myStack);
  
        // Displaying the synchronization
        // status of both Stack
        Console.WriteLine("myStack is {0}.", myStack.IsSynchronized ?
                                "Synchronized" : "Not Synchronized");
  
        Console.WriteLine("ss is {0}.", ss.IsSynchronized ? 
                      "Synchronized" : "Not Synchronized");
    }
}


Output:

myStack is Not Synchronized.
ss is Synchronized.

Example 2:




// C# code to illustrate the
// Stack.IsSynchronized Property
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Stack
        Stack myStack = new Stack();
  
        // Inserting elements into Stack
        myStack.Push("1st");
        myStack.Push("2nd");
        myStack.Push("3rd");
        myStack.Push("4th");
        myStack.Push("5th");
  
        // the default is false for
        // IsSynchronized property
        Console.WriteLine(myStack.IsSynchronized);
    }
}


Output:

False

Reference:



Last Updated : 04 Feb, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads