Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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
InvalidOperationExceptionif 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.
