Queue.Peek Method in C#

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to illustrate the
// Queue.Peek Method
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue("1st Element");
        myQueue.Enqueue("2nd Element");
        myQueue.Enqueue("3rd Element");
        myQueue.Enqueue("4th Element");
        myQueue.Enqueue("5th Element");
        myQueue.Enqueue("6th Element");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
  
        // Displaying the beginning element of Queue
        // without removing it from the Queue
        Console.WriteLine("Element at the beginning is : " 
                                        + myQueue.Peek());
  
        // Displaying the beginning element of Queue
        // without removing it from the Queue
        Console.WriteLine("Element at the beginning is : " 
                                        + myQueue.Peek());
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to illustrate the
// Queue.Peek Method
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue myQueue = new Queue();
  
        // Displaying the beginning element of Queue
        // without removing it from the Queue
        // Calling Peek() method on empty Queue
        // will throw InvalidOperationException.
        Console.WriteLine("Element at the beginning is : "
                                       + myQueue.Peek());
    }
}

chevron_right


Runtime Error:

Unhandled Exception:
System.InvalidOperationException: Queue empty.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.