Getting an object at the beginning of the Queue in C#

The Dequeue() method is used to returns the object at the beginning of the Queue. This method is similar to the Peek() Method. The only difference between Dequeue and Peek method is that Peek() method will not modify the Queue but Dequeue will modify. This method is an O(1) operation and comes under System.Collections.Generic namespace.

Syntax:

public T Dequeue ();

Return value: It returns the object which is removed from the beginning of the Queue.

Exception: The method throws InvalidOperationException on calling empty queue, therefore always check that the total count of a queue is greater than zero before calling the Dequeue() method.

Below programs illustrate the use of the above-discussed method:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate the use
// of Queue<T>.Dequeue Method
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating a queue of integers
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(3);
        queue.Enqueue(2);
        queue.Enqueue(1);
        queue.Enqueue(4);
  
        Console.WriteLine("Number of elements in the Queue: {0}",
                                                   queue.Count);
  
        // Retrieveing top element of queue
        Console.WriteLine("Top element of queue is:");
        Console.WriteLine(queue.Dequeue());
  
        // printing the no of queue element
        // after dequeue operation
        Console.WriteLine("Number of elements in the Queue: {0}",
                                                   queue.Count);
    }
}

chevron_right


Output:

Number of elements in the Queue: 4
Top element of queue is:
3
Number of elements in the Queue: 3

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate the use
// of Queue<T>.Dequeue Method
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        Queue<int> queue = new Queue<int>();
  
        // Adding elements in Queue
        queue.Enqueue(2);
        queue.Enqueue(5);
  
        Console.WriteLine("Number of elements in the Queue: {0}",
                                                    queue.Count);
  
        // Retrieveing top element of queue
        Console.WriteLine("Top element of queue is:");
        Console.WriteLine(queue.Dequeue());
  
        // printing the no. of queue element
        // after dequeue operation
        Console.WriteLine("Number of elements in the Queue: {0}",
                                                   queue.Count);
    }
}

chevron_right


Output:

Number of elements in the Queue: 2
Top element of queue is:
2
Number of elements in the Queue: 1

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.