P
Pavils Jurjans
Hello, please consider the code at the end of this posting, that depicts
simple case of custom enumerator. This particular example will enumerate
over list of random number within 1..10, and will stop once the value of 10
is reached. My question, is what's the real point of implemending the
Reset() method, if it is never used by foreach statement. Of course, it
gives me some allocated place where I can do some "enumerator resetting" and
call the Reset() method from the enumerator constructor, but it's somewhat
strange that the IEnumerator interface demands Reset() to be present, but
never actually cares to call it.
-- Pavils
using System;
using System.Collections;
/*<Settings>
<Outfile>temp</Outfile>
<Run/>
</Settings>*/
class EnumTest : IEnumerable
{
class EnumTestEnumerator : IEnumerator
{
private int rndValue;
private static Random rnd = new Random();
public object Current
{
get
{
Console.WriteLine("Current");
rndValue = rnd.Next(1, 11);
return rndValue;
}
}
public bool MoveNext()
{
Console.WriteLine("MoveNext()");
return rndValue == 0 || rndValue != 10;
}
public void Reset()
{
Console.WriteLine("Reset()");
}
}
public IEnumerator GetEnumerator()
{
return new EnumTestEnumerator();
}
}
class Tester
{
public static void Main()
{
EnumTest et = new EnumTest();
foreach (int value in et)
{
Console.WriteLine(value);
}
Console.Read();
}
}
simple case of custom enumerator. This particular example will enumerate
over list of random number within 1..10, and will stop once the value of 10
is reached. My question, is what's the real point of implemending the
Reset() method, if it is never used by foreach statement. Of course, it
gives me some allocated place where I can do some "enumerator resetting" and
call the Reset() method from the enumerator constructor, but it's somewhat
strange that the IEnumerator interface demands Reset() to be present, but
never actually cares to call it.
-- Pavils
using System;
using System.Collections;
/*<Settings>
<Outfile>temp</Outfile>
<Run/>
</Settings>*/
class EnumTest : IEnumerable
{
class EnumTestEnumerator : IEnumerator
{
private int rndValue;
private static Random rnd = new Random();
public object Current
{
get
{
Console.WriteLine("Current");
rndValue = rnd.Next(1, 11);
return rndValue;
}
}
public bool MoveNext()
{
Console.WriteLine("MoveNext()");
return rndValue == 0 || rndValue != 10;
}
public void Reset()
{
Console.WriteLine("Reset()");
}
}
public IEnumerator GetEnumerator()
{
return new EnumTestEnumerator();
}
}
class Tester
{
public static void Main()
{
EnumTest et = new EnumTest();
foreach (int value in et)
{
Console.WriteLine(value);
}
Console.Read();
}
}