Open In App

Stack.IsSynchronized Property in C#

Last Updated : 04 Feb, 2019
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:



Previous Article
Next Article

Similar Reads

Queue.IsSynchronized Property in C#
This property is used get a value which indicates whether access to the Queue is synchronized (thread safe) or not. Syntax: public virtual bool IsSynchronized { get; } Property Value: This property returns true if access to the Queue is synchronized(thread safe) otherwise, false. The default is false. Below programs illustrate the use of the above-
2 min read
C# ICollection.IsSynchronized Property with Examples
ICollection is an interface that contains size, enumerations, and synchronization methods for all nongeneric collections. It is the base interface for classes in System.Collections namespace. IsSynchronized is also a property of the ICollection interface. This property in C# is defined under System.Collections namespace and a part of System.Runtime
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 Count. Retrieving the value of this property is an
2 min read
Stack.Peek Method in C#
This method(comes under System.Collections namespace) is used to return the object at the top of the Stack without removing it. This method is similar to the Pop method, but Peek does not modify the Stack. Syntax: public virtual object Peek (); Return Value: It returns the Object at the top of the Stack. Exception: Calling Peek() method on empty st
2 min read
C# | Create a Stack from a collection
Stack represents a last-in, first out collection of object. It is used when you need a 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. Creation of stack means the addition of item into the stack. Stack<T>.Push(Object) Method is used to I
3 min read
C# | Check if a Stack contains an element
Stack represents a last-in, first out collection of object. Stack<T>.Contains(Object) Method is used to check whether an element is in the Stack<T> or not. Syntax: public virtual bool Contains(object obj); Return Value: The function returns True if the element exists in the Stack<T> and returns False if the element doesn't exist i
2 min read
C# | Remove all objects from the Stack
Stack represents a last-in, first out collection of object. It is used when you need a 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. Stack<T>.Clear Method is used to Removes all objects from the Stack<T>. This method is an O(n) o
3 min read
C# | Get the number of elements contained in the Stack
Stack represents a last-in, first out collection of object. Stack<T>.Count Property is used to gets the number of elements contained in the Stack. Retrieving the value of this property is an O(1) operation. Syntax: myStack.Count Here myStack is the name of the Stack<T> Return Value: The property returns the number of elements contained
2 min read
C# | Get object at the top of the Stack - Peek operation
Stack represents a last-in, first out collection of object. It is used when you need a 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. Stack<T>.Peek Method is used to returns the object at the top of the Stack<T> without removing i
3 min read
C# | Insert an object at the top of the Stack - Push Operation
Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of 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. Stack<T>.Push(T) Method is used to inserts an object at the top of the Stack<T>. Properties: The
2 min read