Why an Enumerator does not keep track of the item in the same function but not if the MoveNext operation happens in other function ?
Example:
public static void Test()
{
var array = new List<Int32>(new Int32[] { 1, 2, 3, 4, 5 });
var e = array.GetEnumerator();
e.MoveNext();
e.MoveNext();
Console.WriteLine(e.Current); // 2
Incremenet(e);
Console.WriteLine(e.Current); //2
}
static void Incremenet(IEnumerator<Int32> e)
{
Console.WriteLine("Inside " + e.Current); //2
e.MoveNext();
Console.WriteLine("Inside " + e.Current); // 3
e.MoveNext();
Console.WriteLine("Inside " + e.Current); //4
}
I was expecting to get 5 in the last CW, but I get 2, like it was never incremented. Why the MoveNext inside the Increment function are forgotten when the function returns?
Cheers.