I saw the comment of Equidad in one of my answers with an interesting detail and is that implemented an interface (or part of it) without using the operator :
to the right of the name of the object.
Provided the following code:
public class TestEnumerator
{
private int _veces;
public TestEnumerator(int veces) { _veces = veces; }
public object Current { get { return "Hola " + _veces; } }
public bool MoveNext() { return _veces-- > 0; }
}
public class Test
{
public TestEnumerator GetEnumerator() { return new TestEnumerator(10); }
}
public static void Main()
{
var test = new Test();
foreach (var x in test)
Console.WriteLine(x);
}
Indeed, the output is as expected:
Hola 9
Hola 8
Hola 7
Hola 6
Hola 5
Hola 4
Hola 3
Hola 2
Hola 1
Hola 0
What is equivalent to the implementation of a IEnumerable<T>
e IEnumerator
in their respective classes. What is this type of implementation called? Since it is a class with an enumerator, but it is not a collection, I would say that it is a half implementation of IEnumerable
.