Stack.Peek() Method in C#

The Stack.Peek() method in C# is used to return the object at the top of the Stack without removing it. This method is essential when you need to examine the top element while keeping the stack structure intact.

Syntax

Following is the syntax for the Stack.Peek() method −

public virtual object Peek();

Return Value

The method returns the object at the top of the Stack. If the stack is empty, it throws an InvalidOperationException.

Stack.Peek() Operation Item 3 Item 2 Item 1 ? Top Peek() returns this Stack remains unchanged No elements are removed

Using Stack.Peek() Method

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push("Inspiron");
      stack.Push("Alienware");
      stack.Push("Projectors");
      stack.Push("Monitors");
      stack.Push("XPS");
      stack.Push("Laptop");
      stack.Push("Notebook");
      
      Console.WriteLine("Stack elements...");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      
      Console.WriteLine("Count of elements = " + stack.Count);
      Console.WriteLine("Element at the top = " + stack.Peek());
      
      stack.Push("Ultrabook");
      stack.Push("Cameras");
      stack.Push("Keyboards");
      
      Console.WriteLine("\nStack elements...updated");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      
      Console.WriteLine("Element at the top = " + stack.Peek());
      Console.WriteLine("Count of elements (updated) = " + stack.Count);
   }
}

The output of the above code is −

Stack elements...
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Count of elements = 7
Element at the top = Notebook

Stack elements...updated
Keyboards
Cameras
Ultrabook
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Element at the top = Keyboards
Count of elements (updated) = 10

Peek() with Exception Handling

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      
      // Try peeking an empty stack
      try {
         Console.WriteLine("Peeking empty stack: " + stack.Peek());
      }
      catch (InvalidOperationException ex) {
         Console.WriteLine("Exception: " + ex.Message);
      }
      
      // Add elements and peek
      stack.Push("First");
      stack.Push("Second");
      stack.Push("Third");
      
      Console.WriteLine("\nAfter adding elements:");
      Console.WriteLine("Top element: " + stack.Peek());
      Console.WriteLine("Stack count: " + stack.Count);
      
      // Peek multiple times - stack remains unchanged
      Console.WriteLine("First peek: " + stack.Peek());
      Console.WriteLine("Second peek: " + stack.Peek());
      Console.WriteLine("Stack count after multiple peeks: " + stack.Count);
   }
}

The output of the above code is −

Exception: Stack empty.

After adding elements:
Top element: Third
Stack count: 3
First peek: Third
Second peek: Third
Stack count after multiple peeks: 3

Key Points

  • Peek() returns the top element without removing it from the stack.

  • The method throws InvalidOperationException if called on an empty stack.

  • Multiple calls to Peek() return the same element until the stack is modified.

  • The stack count remains unchanged after calling Peek().

Conclusion

The Stack.Peek() method provides a safe way to examine the top element of a stack without modifying its structure. Always handle the InvalidOperationException when working with potentially empty stacks to avoid runtime errors.

Updated on: 2026-03-17T07:04:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements