Stack Class Home
This page is specific to:.NET Framework Version:1.12.03.03.54.0
.NET Framework Class Library
Stack Class

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

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

'Usage

Dim instance As Stack

'Declaration

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class Stack _
    Implements ICollection, IEnumerable, ICloneable
Remarks

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

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

Examples

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

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesStack    

    Public Shared Sub Main()

        ' Creates and initializes a new Stack.
        Dim myStack As New Stack()
        myStack.Push("Hello")
        myStack.Push("World")
        myStack.Push("!")

        ' Displays the properties and values of the Stack.
        Console.WriteLine("myStack")
        Console.WriteLine(ControlChars.Tab & "Count:    {0}", myStack.Count)
        Console.Write(ControlChars.Tab & "Values:")
        PrintValues(myStack)
    End Sub

    Public Shared Sub PrintValues(myCollection As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myCollection
            Console.Write("    {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues

End Class

' This code produces the following output.
'
' myStack
'     Count:     3
'     Values:    !    World    Hello


Inheritance Hierarchy

System..::.Object
  System.Collections..::.Stack
    Microsoft.VisualC..::.SymbolTableStack
Thread Safety

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.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Community Content

Stack Sample using PowerShell
Added by:Thomas Lee
# Get-Stack.ps1
# Stack Sample using PowerShell
# Thomas Lee - tfl@psp.co.uk
 
$myStack = new-object  System.Collections.Stack 
$myStack.Push("Hello")
$myStack.Push("World")
$myStack.Push("!")

# Display the properties and values of the Stack
"mystack"
"Count: {0}" -f $myStack.Count
"Values:"
$mystack

This script produces the following output

PSH [D:\foo]: .\get-stack.ps1
myStack
Count: 3
Values:
!
World
Hello
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View