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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


Output:

myStack is Not Synchronized.
ss is Synchronized.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

False

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.