VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/queue-peek-method-in-c-sharp/

⇱ Queue.Peek Method in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Queue.Peek Method in C#

Last Updated : 11 Jul, 2025
This method returns the object at the beginning of the Queue without removing it. This method is similar to the Dequeue method, but Peek does not modify the Queue and is an O(1) operation. This method comes under System.Collections namespace. Syntax:
public virtual object Peek ();
Return Value: The object at the beginning of the Queue. Exception: This method will give InvalidOperationException if the Queue is empty. Below programs illustrate the use of above-discussed method: Example 1: Output:
Total number of elements in the Queue are : 6
Element at the beginning is : 1st Element
Element at the beginning is : 1st Element
Total number of elements in the Queue are : 6
Example 2:
Runtime Error:
Unhandled Exception: System.InvalidOperationException: Queue empty.
Reference:
Comment

Explore