LINQ Interview Question #12
What is an IEnumerator<T>?
LINQ Fundamentals, Syntax, IEnumerable & IQueryable Junior Beginner
Quick Interview Answer
IEnumerator<T> is the object that performs iteration over an IEnumerable<T> sequence. MoveNext advances to the next element, and Current exposes the current value.
Detailed Explanation
IEnumerator<T> is the object that performs iteration over an IEnumerable<T> sequence. MoveNext advances to the next element, and Current exposes the current value.
Developers usually use foreach or LINQ rather than manipulating an enumerator directly, but understanding the enumerator model helps explain deferred execution and how sequences are consumed.
Code Example
using IEnumerator<int> iterator =
numbers.GetEnumerator();
while (iterator.MoveNext())
{
Console.WriteLine(iterator.Current);
}