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.GetEnumerator() Method in C#
The Queue.GetEnumerator() method in C# returns an IEnumerator object that allows you to iterate through the elements of a Queue in FIFO (First In, First Out) order. This method is essential for implementing custom iteration logic or using the Queue in foreach loops.
Syntax
Following is the syntax for the GetEnumerator() method −
public virtual System.Collections.IEnumerator GetEnumerator();
Return Value
The method returns an IEnumerator object that can iterate through the Queue elements from the front to the rear.
Using GetEnumerator() with While Loop
The most common way to use GetEnumerator() is with a while loop using MoveNext() and Current properties −
using System;
using System.Collections;
public class Demo {
public static void Main() {
Queue queue = new Queue();
queue.Enqueue(100);
queue.Enqueue(200);
queue.Enqueue(300);
queue.Enqueue(400);
Console.WriteLine("Queue elements:");
IEnumerator enumerator = queue.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
Console.WriteLine("Queue count: " + queue.Count);
}
}
The output of the above code is −
Queue elements: 100 200 300 400 Queue count: 4
Using GetEnumerator() with Mixed Data Types
Since Queue is non-generic, it can store different data types. The enumerator handles this seamlessly −
using System;
using System.Collections;
public class Demo {
public static void Main() {
Queue queue = new Queue();
queue.Enqueue("First");
queue.Enqueue(42);
queue.Enqueue(3.14);
queue.Enqueue('A');
Console.WriteLine("Mixed data types in Queue:");
IEnumerator enumerator = queue.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine($"Value: {enumerator.Current}, Type: {enumerator.Current.GetType().Name}");
}
}
}
The output of the above code is −
Mixed data types in Queue: Value: First, Type: String Value: 42, Type: Int32 Value: 3.14, Type: Double Value: A, Type: Char
GetEnumerator() vs Foreach Loop
While GetEnumerator() provides explicit control, foreach loops use it internally for cleaner syntax −
using System;
using System.Collections;
public class Demo {
public static void Main() {
Queue queue = new Queue();
queue.Enqueue("Apple");
queue.Enqueue("Banana");
queue.Enqueue("Cherry");
Console.WriteLine("Using GetEnumerator():");
IEnumerator enumerator = queue.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
Console.WriteLine("\nUsing foreach (internally uses GetEnumerator()):");
foreach (object item in queue) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Using GetEnumerator(): Apple Banana Cherry Using foreach (internally uses GetEnumerator()): Apple Banana Cherry
Key Points
-
The enumerator iterates elements in FIFO order (same as dequeue order).
-
The enumerator is a snapshot − modifying the Queue during enumeration throws an exception.
-
MoveNext()returnstrueif there are more elements,falseotherwise. -
Currentproperty returns the current element without removing it from the Queue.
Conclusion
The Queue.GetEnumerator() method provides explicit control over iteration through Queue elements in FIFO order. While foreach loops are more convenient for simple iterations, GetEnumerator() is useful when you need custom iteration logic or want to understand how enumeration works internally.
