I did the work below
class Program
{
class GenEnumerator<T> : IEnumerable
{
public T[] Values { get; set; }
public IEnumerator GetEnumerator()
{
for (int i = 0; i < Values.Length; i++)
yield return Values[i];
}
}
static void Main(string[] args)
{
GenEnumerator<string> g = new GenEnumerator<string>();
g.Values = new string[] { "a", "b", "c", "d", "e" };
g.GetEnumerator().MoveNext();
Console.WriteLine(g.GetEnumerator().Current);
Console.ReadKey();
}
}
g.GetEnumerator().Currentalways zero.
but if I do - var a = g.GetEnumerator();
Current proeperty gets the value and works fine
Does this mean that I should explicitly inherit the class from IEnumeratorand implement its methods and property if I want to use Currentwithout a type variable IEnumerator?
Thanks Roy
source
share