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
Get object at the top of the Stack in C#
The Stack<T> collection in C# follows the Last In, First Out (LIFO) principle. To get the object at the top of the stack without removing it, we use the Peek() method. This method returns the top element but keeps it in the stack, unlike Pop() which removes the element.
Syntax
Following is the syntax for the Peek() method −
public T Peek()
Return Value
The Peek() method returns the object at the top of the stack of type T. It throws an InvalidOperationException if the stack is empty.
Using Peek() with String Stack
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Stack<string> stack = new Stack<string>();
stack.Push("A");
stack.Push("B");
stack.Push("C");
stack.Push("D");
stack.Push("E");
stack.Push("F");
stack.Push("G");
stack.Push("H");
stack.Push("I");
stack.Push("J");
Console.WriteLine("Count of elements = " + stack.Count);
Console.WriteLine("Element at the top of stack = " + stack.Peek());
Console.WriteLine("Count after Peek() = " + stack.Count);
}
}
The output of the above code is −
Count of elements = 10 Element at the top of stack = J Count after Peek() = 10
Using Peek() with Integer Stack
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Stack<int> stack = new Stack<int>();
stack.Push(10);
stack.Push(20);
stack.Push(30);
stack.Push(40);
stack.Push(50);
stack.Push(60);
stack.Push(70);
stack.Push(80);
stack.Push(90);
stack.Push(100);
Console.WriteLine("Count of elements = " + stack.Count);
Console.WriteLine("Element at the top of stack = " + stack.Peek());
Console.WriteLine("Element at the top of stack = " + stack.Peek());
Console.WriteLine("Stack count remains = " + stack.Count);
}
}
The output of the above code is −
Count of elements = 10 Element at the top of stack = 100 Element at the top of stack = 100 Stack count remains = 10
Peek() vs Pop() Comparison
| Peek() | Pop() |
|---|---|
| Returns the top element without removing it | Returns the top element and removes it |
| Stack count remains unchanged | Stack count decreases by 1 |
| Can be called multiple times on same element | Each call removes a different element |
| Throws InvalidOperationException if stack is empty | Throws InvalidOperationException if stack is empty |
Handling Empty Stack
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Stack<string> stack = new Stack<string>();
if (stack.Count > 0) {
Console.WriteLine("Top element: " + stack.Peek());
} else {
Console.WriteLine("Stack is empty - cannot peek");
}
stack.Push("First");
Console.WriteLine("After adding element: " + stack.Peek());
}
}
The output of the above code is −
Stack is empty - cannot peek After adding element: First
Conclusion
The Peek() method allows you to inspect the top element of a Stack without modifying the collection. It's essential to check if the stack is empty before calling Peek() to avoid exceptions, and remember that multiple calls to Peek() return the same element since it doesn't remove anything.
