Following two methods (one uses IEnumerator<int>
, and other uses List<int>.Enumerator
) even though looks identical produces different results.
static void M1()
{
var list = new List<int>() { 1, 2, 3, 4 };
IEnumerator<int> iterator = list.GetEnumerator();
while (iterator.MoveNext())
{
Console.Write(iterator.Current);
}
iterator.Reset();
while (iterator.MoveNext())
{
Console.Write(iterator.Current);
}
}
static void M2()
{
var list = new List<int>() { 1, 2, 3, 4 };
//Here the iterator will be List<int>.Enumerator (which is a struct)
var iterator = list.GetEnumerator();
while (iterator.MoveNext())
{
Console.Write(iterator.Current);
}
//This will not work, as Reset method was implemented explicitly
//iterator.Reset();
//So casting it to IEnumerator is required
//which will lead to boxing and other issues of struct and interface
((IEnumerator<int>)iterator).Reset();
//Following loop will NOT work
while (iterator.MoveNext())
{
Console.Write(iterator.Current);
}
}
There are couple of questions which clearly explains this behavior, you can check them here, here, and here.
I still have following two doubts
- Why List.Enumerator does not throw "NotSupportedException" for Reset?
- Why Reset was implemented explicitly and not implicitly like MoveNext and Current?