Stack Class

Stack Class

 

Represents a simple last-in-first-out (LIFO) non-generic collection of objects.

Namespace:   System.Collections
Assembly:  mscorlib (in mscorlib.dll)

System.Object
  System.Collections.Stack

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class Stack : ICollection, IEnumerable, ICloneable

NameDescription
System_CAPS_pubmethodStack()

Initializes a new instance of the Stack class that is empty and has the default initial capacity.

System_CAPS_pubmethodStack(ICollection)

Initializes a new instance of the Stack class that contains elements copied from the specified collection and has the same initial capacity as the number of elements copied.

System_CAPS_pubmethodStack(Int32)

Initializes a new instance of the Stack class that is empty and has the specified initial capacity or the default initial capacity, whichever is greater.

NameDescription
System_CAPS_pubpropertyCount

Gets the number of elements contained in the Stack.

System_CAPS_pubpropertyIsSynchronized

Gets a value indicating whether access to the Stack is synchronized (thread safe).

System_CAPS_pubpropertySyncRoot

Gets an object that can be used to synchronize access to the Stack.

NameDescription
System_CAPS_pubmethodClear()

Removes all objects from the Stack.

System_CAPS_pubmethodClone()

Creates a shallow copy of the Stack.

System_CAPS_pubmethodContains(Object)

Determines whether an element is in the Stack.

System_CAPS_pubmethodCopyTo(Array, Int32)

Copies the Stack to an existing one-dimensional Array, starting at the specified array index.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetEnumerator()

Returns an IEnumerator for the Stack.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodPeek()

Returns the object at the top of the Stack without removing it.

System_CAPS_pubmethodPop()

Removes and returns the object at the top of the Stack.

System_CAPS_pubmethodPush(Object)

Inserts an object at the top of the Stack.

System_CAPS_pubmethodSystem_CAPS_staticSynchronized(Stack)

Returns a synchronized (thread safe) wrapper for the Stack.

System_CAPS_pubmethodToArray()

Copies the Stack to a new array.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast<TResult>()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType<TResult>()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

For the generic version of this collection, see System.Collections.Generic.Stack<T>.

The capacity of a Stack is the number of elements the Stack can hold. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.

If Count is less than the capacity of the stack, Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Push becomes an O(n) operation, where n is Count. Pop is an O(1) operation.

Stack accepts null as a valid value and allows duplicate elements.

The following example shows how to create and add values to a Stack and how to display its values.

using System;
using System.Collections;
public class SamplesStack  {

   public static void Main()  {

      // Creates and initializes a new Stack.
      Stack myStack = new Stack();
      myStack.Push("Hello");
      myStack.Push("World");
      myStack.Push("!");

      // Displays the properties and values of the Stack.
      Console.WriteLine( "myStack" );
      Console.WriteLine( "\tCount:    {0}", myStack.Count );
      Console.Write( "\tValues:" );
      PrintValues( myStack );
   }

   public static void PrintValues( IEnumerable myCollection )  {
      foreach ( Object obj in myCollection )
         Console.Write( "    {0}", obj );
      Console.WriteLine();
   }

}


/* 
This code produces the following output.

myStack
    Count:    3
    Values:    !    World    Hello
*/ 

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

To guarantee the thread safety of the Stack, all operations must be done through the wrapper returned by the Synchronized method.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Return to top
Show:
© 2016 Microsoft