C
cody
Currently it is only legal to use types which has a method named
GetEnumerator to be used in a foreach loop.
This makes it impossible to use the same Enumerator after and before a
foreach loop, because GetEnumerator always returns a new one.
My proposal would allow the following scenario:
You could for example the first and the last element of the list as a
special case:
ArrayList l = new ArrayList();
l.Add(1);
l.Add(2);
l.Add(3);
IEnumerator e = l.GetEnumerator();
if (e.MoveNext())
{
object first = e.current;
// do something with first element
}
// reuse enumerator from above, note that iteration starts with second
// element, not the first, unless we call Reset on the enumerator.
foreach object i in e)
{
// do something
}
object last = e.Current; // current still points to last element
// do something with the last element
GetEnumerator to be used in a foreach loop.
This makes it impossible to use the same Enumerator after and before a
foreach loop, because GetEnumerator always returns a new one.
My proposal would allow the following scenario:
You could for example the first and the last element of the list as a
special case:
ArrayList l = new ArrayList();
l.Add(1);
l.Add(2);
l.Add(3);
IEnumerator e = l.GetEnumerator();
if (e.MoveNext())
{
object first = e.current;
// do something with first element
}
// reuse enumerator from above, note that iteration starts with second
// element, not the first, unless we call Reset on the enumerator.
foreach object i in e)
{
// do something
}
object last = e.Current; // current still points to last element
// do something with the last element