C# Stack Peek

Description

Stack Peek returns the object at the top of the Stack without removing it.

Syntax

Stack.Peek has the following syntax.


public virtual Object Peek()

Returns

Stack.Peek method returns The Object at the top of the Stack.

Example

The following example shows how to add elements to the Stack and view the element at the top of the Stack.


using System;/*from   w  w w.j  av a  2 s  .  c  om*/
 using System.Collections;
 public class SamplesStack  {

    public static void Main()  {
       Stack myStack = new Stack();
       myStack.Push( "A" );
       myStack.Push( "B" );
       myStack.Push( "C" );

       Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() );
    }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack