The usual way to use an IEnumerator is through the 'foreach' construct
This approach makes it impossible to 'move back'. If you wanted to be able to invoke MoveBack, you'd have to do something like:Code:foreach (MyObject m in list) { DoStuff (); }
Where customenumerator is:Code:CustomEnumerator e = (CustomEnumerator) list.GetEnumerator (); while (e.MoveNext ()) { MyObject m = e.Current; DoStuff (m); if (condition == true) e.MoveBack (); }
and you also implement a class which inherits this interface and fulfills the contract and you also implement a list which returns an instance of this custom enumerator when you call 'GetEnumerator'.Code:interface ICustomEnumerator : IEnumerator { bool MoveBack (); }
It's far far easier to use a standard 'for' loop
If you can't index the collection in question, you could always just do:Code:for (int i=0; i < list.Count; i++) { MyObject m = list[i]; DoStuff (m); if (condition == true) i--; }
List<MyObject> list = new List<MyObject> (existingEnumerable); and so gain indexing on the collection.





Reply With Quote