This documentation is archived and is not being maintained.

Stack(Of T) Class

Represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.

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

<SerializableAttribute> _
<ComVisibleAttribute(False)> _
Public Class Stack(Of T) _
	Implements IEnumerable(Of T), ICollection, IEnumerable
Dim instance As Stack(Of T)

Type Parameters


Specifies the type of elements in the stack.

Stack(Of T) is implemented as an array.

The capacity of a Stack(Of T) is the number of elements the Stack(Of T) can hold. As elements are added to a Stack(Of T), the capacity is automatically increased as required by reallocating the internal array. The capacity can be decreased by calling TrimExcess.

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(Of T) accepts Nothing as a valid value for reference types and allows duplicate elements.

The following code example demonstrates several methods of the Stack(Of T) generic class. The code example creates a stack of strings with default capacity and uses the Push method to push five strings onto the stack. The elements of the stack are enumerated, which does not change the state of the stack. The Pop method is used to pop the first string off the stack. The Peek method is used to look at the next item on the stack, and then the Pop method is used to pop it off.

The ToArray method is used to create an array and copy the stack elements to it, then the array is passed to the Stack(Of T) constructor that takes IEnumerable(Of T), creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.

An array twice the size of the stack is created, and the CopyTo method is used to copy the array elements beginning at the middle of the array. The Stack(Of T) constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.

The Contains method is used to show that the string "four" is in the first copy of the stack, after which the Clear method clears the copy and the Count property shows that the stack is empty.

Imports System
Imports System.Collections.Generic

Module Example

    Sub Main

        Dim numbers As New Stack(Of String)

        ' A stack can be enumerated without disturbing its contents. 
        For Each number As String In numbers

        Console.WriteLine(vbLf & "Popping '{0}'", numbers.Pop())
        Console.WriteLine("Peek at next item to pop: {0}", _
        Console.WriteLine("Popping '{0}'", numbers.Pop())

        ' Create another stack, using the ToArray method and the 
        ' constructor that accepts an IEnumerable(Of T). Note that 
        ' the order of items on the new stack is reversed. 
        Dim stack2 As New Stack(Of String)(numbers.ToArray())

        Console.WriteLine(vbLf & "Contents of the first copy:")
        For Each number As String In stack2

        ' Create an array twice the size of the stack, compensating 
        ' for the fact that Visual Basic allocates an extra array  
        ' element. Copy the elements of the stack, starting at the 
        ' middle of the array.  
        Dim array2((numbers.Count * 2) - 1) As String
        numbers.CopyTo(array2, numbers.Count)

        ' Create a second stack, using the constructor that accepts an 
        ' IEnumerable(Of T). The elements are reversed, with the null 
        ' elements appearing at the end of the stack when enumerated. 
        Dim stack3 As New Stack(Of String)(array2)

        Console.WriteLine(vbLf & _
            "Contents of the second copy, with duplicates and nulls:")
        For Each number As String In stack3

        Console.WriteLine(vbLf & "stack2.Contains(""four"") = {0}", _

        Console.WriteLine(vbLf & "stack2.Clear()")
        Console.WriteLine(vbLf & "stack2.Count = {0}", _
    End Sub 
End Module 

' This code example produces the following output: 


'Popping 'five' 
'Peek at next item to pop: four 
'Popping 'four' 

'Contents of the first copy: 

'Contents of the second copy, with duplicates and nulls: 

'stack2.Contains("four") = False 


'stack2.Count = 0

  System.Collections.Generic.Stack(Of T)

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

A Stack(Of T) can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

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.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0