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
Queue.Dequeue Method in C#
The Queue.Dequeue() method in C# is used to remove and return the object at the beginning of the Queue. This method follows the FIFO (First In, First Out) principle, where the first element added to the queue is the first one to be removed.
Syntax
Following is the syntax for the Dequeue() method −
public T Dequeue();
Return Value
The method returns the object that is removed from the beginning of the Queue. If the queue is empty, it throws an InvalidOperationException.
Using Dequeue() to Remove Elements
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<string> queue = new Queue<string>();
queue.Enqueue("A");
queue.Enqueue("B");
queue.Enqueue("C");
queue.Enqueue("D");
queue.Enqueue("E");
queue.Enqueue("F");
queue.Enqueue("G");
Console.WriteLine("Count of elements = " + queue.Count);
Console.WriteLine("Element at the beginning of queue = " + queue.Peek());
// Remove three elements and show which ones were removed
Console.WriteLine("Removed: " + queue.Dequeue());
Console.WriteLine("Removed: " + queue.Dequeue());
Console.WriteLine("Removed: " + queue.Dequeue());
Console.WriteLine("Count of elements = " + queue.Count);
}
}
The output of the above code is −
Count of elements = 7 Element at the beginning of queue = A Removed: A Removed: B Removed: C Count of elements = 4
Using Dequeue() with Queue Processing
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<string> queue = new Queue<string>();
queue.Enqueue("Gary");
queue.Enqueue("Jack");
queue.Enqueue("Ryan");
queue.Enqueue("Kevin");
queue.Enqueue("Mark");
queue.Enqueue("Alice");
Console.WriteLine("Count of elements = " + queue.Count);
Console.WriteLine("Original Queue:");
foreach(string i in queue) {
Console.WriteLine(i);
}
// Process first two elements
string first = queue.Dequeue();
string second = queue.Dequeue();
Console.WriteLine("\nProcessed: " + first + " and " + second);
Console.WriteLine("\nUpdated Queue:");
foreach(string i in queue) {
Console.WriteLine(i);
}
Console.WriteLine("Count of elements (updated) = " + queue.Count);
}
}
The output of the above code is −
Count of elements = 6 Original Queue: Gary Jack Ryan Kevin Mark Alice Processed: Gary and Jack Updated Queue: Ryan Kevin Mark Alice Count of elements (updated) = 4
Exception Handling with Empty Queue
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<int> queue = new Queue<int>();
// Safe dequeue using Count check
if (queue.Count > 0) {
Console.WriteLine("Dequeued: " + queue.Dequeue());
} else {
Console.WriteLine("Queue is empty - cannot dequeue");
}
// Add elements and then dequeue safely
queue.Enqueue(10);
queue.Enqueue(20);
while (queue.Count > 0) {
Console.WriteLine("Dequeued: " + queue.Dequeue());
}
Console.WriteLine("Queue is now empty. Count: " + queue.Count);
}
}
The output of the above code is −
Queue is empty - cannot dequeue Dequeued: 10 Dequeued: 20 Queue is now empty. Count: 0
Conclusion
The Queue.Dequeue() method removes and returns the element at the front of the queue, maintaining FIFO order. Always check if the queue contains elements using Count property before calling Dequeue() to avoid exceptions when the queue is empty.
