Stack Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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

System..::..Object
  System.Collections..::..Stack

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

[SerializableAttribute]
public class Stack : ICollection, IEnumerable, 
	ICloneable

The Stack type exposes the following members.

  NameDescription
Public methodStackInitializes a new instance of the Stack class that is empty and has the default initial capacity.
Top

  NameDescription
Public propertyCountGets the number of elements contained in the Stack.
Public propertyIsSynchronizedGets a value indicating whether access to the Stack is synchronized (thread safe).
Public propertySyncRootGets an object that can be used to synchronize access to the Stack.
Top

  NameDescription
Public methodClearRemoves all objects from the Stack.
Public methodCloneCreates a shallow copy of the Stack.
Public methodContainsDetermines whether an element is in the Stack.
Public methodCopyToCopies the Stack to an existing one-dimensional Array, starting at the specified array index.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorReturns an IEnumerator for the Stack.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodPeekReturns the object at the top of the Stack without removing it.
Public methodPopRemoves and returns the object at the top of the Stack.
Public methodPushInserts an object at the top of the Stack.
Public methodToArrayCopies the Stack to a new array.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Stack is implemented as a circular buffer.

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 nullNothingnullptrunita null reference (Nothing in Visual Basic) as a valid value and allows duplicate elements.

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