I used it sometimes when the first iteration do something different from others.
For example, if you want to print members to console and separate results by line, you can write.
using (IEnumerator<MyClass> classesEnum = myClasses.GetEnumerator()) {
if (classEnum.MoveNext())
Console.WriteLine(classesEnum.Current);
while (classesEnum.MoveNext()) {
Console.WriteLine("-----------------");
Console.WriteLine(classesEnum.Current);
}
}
Then the result is
myClass 1
-----------------
myClass 2
-----------------
myClass 3
And another situation is when I iterate 2 enumerators together.
using (IEnumerator<MyClass> classesEnum = myClasses.GetEnumerator()) {
using (IEnumerator<MyClass> classesEnum2 = myClasses2.GetEnumerator()) {
while (classesEnum.MoveNext() && classEnum2.MoveNext())
Console.WriteLine("{0}, {1}", classesEnum.Current, classesEnum2.Current);
}
}
result
myClass 1, myClass A
myClass 2, myClass B
myClass 3, myClass C
Using enumerator allows you to do more flexible on your iteration. But in most case, you can use foreach