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
Get an enumerator that iterates through the List in C#
In C#, you can get an enumerator that iterates through a List<T> using the GetEnumerator() method. An enumerator provides a way to access each element in a collection sequentially without exposing the underlying structure. The List<T> class implements IEnumerable<T>, which provides enumerator functionality.
Syntax
Following is the syntax to get an enumerator from a List −
List<T>.Enumerator enumerator = list.GetEnumerator();
Following is the syntax to iterate using the enumerator −
while (enumerator.MoveNext()) {
T currentElement = enumerator.Current;
// process currentElement
}
How It Works
The enumerator uses two key members −
MoveNext()− Advances the enumerator to the next element and returnstrueif successful,falseif at the end.Current− Gets the element at the current position of the enumerator.
Using GetEnumerator() Method
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<String> list1 = new List<String>();
list1.Add("One");
list1.Add("Two");
list1.Add("Three");
list1.Add("Four");
list1.Add("Five");
Console.WriteLine("Elements in List1...");
foreach (string res in list1) {
Console.WriteLine(res);
}
List<String> list2 = new List<String>();
list2.Add("India");
list2.Add("US");
list2.Add("UK");
list2.Add("Canada");
list2.Add("Poland");
list2.Add("Netherlands");
Console.WriteLine("Elements in List2...");
List<String>.Enumerator demoEnum = list2.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
Console.WriteLine("Is List2 equal to List1? = " + list2.Equals(list1));
}
}
The output of the above code is −
Elements in List1... One Two Three Four Five Elements in List2... India US UK Canada Poland Netherlands Is List2 equal to List1? = False
Using Enumerator with Different Data Types
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
numbers.Add(40);
numbers.Add(50);
Console.WriteLine("Enumerator iterates through the integer list...");
List<int>.Enumerator numberEnum = numbers.GetEnumerator();
while (numberEnum.MoveNext()) {
int currentNumber = numberEnum.Current;
Console.WriteLine("Number: " + currentNumber + ", Square: " + (currentNumber * currentNumber));
}
}
}
The output of the above code is −
Enumerator iterates through the integer list... Number: 10, Square: 100 Number: 20, Square: 400 Number: 30, Square: 900 Number: 40, Square: 1600 Number: 50, Square: 2500
Comparison: Enumerator vs foreach
| Enumerator (GetEnumerator) | foreach Loop |
|---|---|
| Provides explicit control over iteration | Automatic iteration with simpler syntax |
| Requires manual MoveNext() and Current calls | Handles enumeration internally |
| More verbose but offers fine-grained control | Cleaner code for simple iterations |
| Can pause and resume iteration | Cannot pause mid-iteration |
Conclusion
The GetEnumerator()MoveNext() and Current properties. While foreach is simpler for basic iterations, enumerators offer fine-grained control when you need to pause, resume, or conditionally process elements during iteration.
