Export (0) Print
Expand All
4 out of 6 rated this helpful - Rate this topic

Stack Class

Represents a simple last-in-first-out collection of objects.

For a list of all members of this type, see Stack Members.

System.Object
   System.Collections.Stack

[Visual Basic]
<Serializable>
Public Class Stack
   Implements ICollection, IEnumerable, ICloneable
[C#]
[Serializable]
public class Stack : ICollection, IEnumerable, ICloneable
[C++]
[Serializable]
public __gc class Stack : public ICollection, IEnumerable,
   ICloneable
[JScript]
public
   Serializable
class Stack implements ICollection, IEnumerable, ICloneable

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. 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 could 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.

Remarks

Stack is implemented as a circular buffer.

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

Example

[Visual Basic, C#, C++] The following example shows how to create and add values to a Stack and how to print out its values.

[Visual Basic] 
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 myEnumerator As System.Collections.IEnumerator = _
           myCollection.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write(ControlChars.Tab & "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

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

[C#] 
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 )  {
      System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "\t{0}", myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

myStack
    Count:    3
    Values:    !    World    Hello
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

public __gc class SamplesStack  {
public:
   static void PrintValues(IEnumerable __gc *myCollection)  {
      System::Collections::IEnumerator __gc *myEnumerator = myCollection->GetEnumerator();
      while (myEnumerator->MoveNext())
         Console::Write(S"\t{0}", myEnumerator->Current);
      Console::WriteLine();
   }
};

int main()  {

   // Creates and initializes a new Stack.
   Stack __gc *myStack = new Stack();
   myStack->Push(S"Hello");
   myStack->Push(S"World");
   myStack->Push(S"!");

   // Displays the properties and values of the Stack.
   Console::WriteLine(S"myStack");
   Console::WriteLine(S"\tCount:    {0}", __box(myStack->Count));
   Console::Write(S"\tValues:");
   SamplesStack::PrintValues(myStack);
}
/* 
This code produces the following output.

myStack
        Count:    3
        Values: !       World   Hello
*/ 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Collections

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Stack Members | System.Collections Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.