Export (0) Print
Expand All

Stack.Peek Method

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

[Visual Basic]
Public Overridable Function Peek() As Object
[C#]
public virtual object Peek();
[C++]
public: virtual Object* Peek();
[JScript]
public function Peek() : Object;

Return Value

The Object at the top of the Stack.

Exceptions

Exception Type Condition
InvalidOperationException The Stack is empty.

Remarks

This method is similar to the Pop method, but Peek does not modify the Stack.

A null reference (Nothing in Visual Basic) can be pushed onto the Stack as a placeholder, if needed. To distinguish between a null value and the end of the stack, check the Count property or catch the InvalidOperationException, which is thrown when the Stack is empty.

Example

[Visual Basic, C#, C++] The following example shows how to add elements to the Stack, remove elements from the Stack or just view the element at the top of the Stack.

[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("The")
        myStack.Push("quick")
        myStack.Push("brown")
        myStack.Push("fox")
        
        ' Displays the Stack.
        Console.Write("Stack values:")
        PrintValues(myStack, ControlChars.Tab)
        
        ' Removes an element from the Stack.
        Console.WriteLine("(Pop)" & ControlChars.Tab & ControlChars.Tab & _
           "{0}", myStack.Pop())
        
        ' Displays the Stack.
        Console.Write("Stack values:")
        PrintValues(myStack, ControlChars.Tab)
        
        ' Removes another element from the Stack.
        Console.WriteLine("(Pop)" & ControlChars.Tab & ControlChars.Tab & _
           "{0}", myStack.Pop())
        
        ' Displays the Stack.
        Console.Write("Stack values:")
        PrintValues(myStack, ControlChars.Tab)
        
        ' Views the first element in the Stack but does not remove it.
        Console.WriteLine("(Peek)" & ControlChars.Tab & ControlChars.Tab & _
           "{0}", myStack.Peek())
        
        ' Displays the Stack.
        Console.Write("Stack values:")
        PrintValues(myStack, ControlChars.Tab)
    End Sub
    
    Public Shared Sub PrintValues(myCollection As IEnumerable, _
       mySeparator As Char)
       
        Dim myEnumerator As System.Collections.IEnumerator = _
           myCollection.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Stack values:    fox    brown    quick    The
' (Pop)        fox
' Stack values:    brown    quick    The
' (Pop)        brown
' Stack values:    quick    The
' (Peek)        quick
' Stack values:    quick    The
 

[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( "The" );
      myStack.Push( "quick" );
      myStack.Push( "brown" );
      myStack.Push( "fox" );

      // Displays the Stack.
      Console.Write( "Stack values:" );
      PrintValues( myStack, '\t' );

      // Removes an element from the Stack.
      Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );

      // Displays the Stack.
      Console.Write( "Stack values:" );
      PrintValues( myStack, '\t' );

      // Removes another element from the Stack.
      Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );

      // Displays the Stack.
      Console.Write( "Stack values:" );
      PrintValues( myStack, '\t' );

      // Views the first element in the Stack but does not remove it.
      Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() );

      // Displays the Stack.
      Console.Write( "Stack values:" );
      PrintValues( myStack, '\t' );
   }


   public static void PrintValues( IEnumerable myCollection, char mySeparator )  {
      System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Stack values:    fox    brown    quick    The
(Pop)        fox
Stack values:    brown    quick    The
(Pop)        brown
Stack values:    quick    The
(Peek)        quick
Stack values:    quick    The
*/ 

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

using namespace System;
using namespace System::Collections;


void PrintValues( IEnumerable* myCollection, Char mySeparator )  {
   System::Collections::IEnumerator* myEnumerator = myCollection->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::Write( S"{0}{1}", __box(mySeparator), myEnumerator->Current );
   Console::WriteLine();
}

int main()  {

   // Creates and initializes a new Stack.
   Stack* myStack = new Stack();
   myStack->Push( S"The" );
   myStack->Push( S"quick" );
   myStack->Push( S"brown" );
   myStack->Push( S"fox" );

   // Displays the Stack.
   Console::Write( S"Stack values:" );
   PrintValues( myStack, '\t' );

   // Removes an element from the Stack.
   Console::WriteLine( S"(Pop)\t\t{0}", myStack->Pop() );

   // Displays the Stack.
   Console::Write( S"Stack values:" );
   PrintValues( myStack, '\t' );

   // Removes another element from the Stack.
   Console::WriteLine( S"(Pop)\t\t{0}", myStack->Pop() );

   // Displays the Stack.
   Console::Write( S"Stack values:" );
   PrintValues( myStack, '\t' );

   // Views the first element in the Stack but does not remove it.
   Console::WriteLine( S"(Peek)\t\t{0}", myStack->Peek() );

   // Displays the Stack.
   Console::Write( S"Stack values:" );
   PrintValues( myStack, '\t' );
}

/*
This code produces the following output.

Stack values:   fox     brown   quick   The
(Pop)           fox
Stack values:   brown   quick   The
(Pop)           brown
Stack values:   quick   The
(Peek)          quick
Stack values:   quick   The
*/

[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

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

See Also

Stack Class | Stack Members | System.Collections Namespace | Pop | Push

Show:
© 2014 Microsoft