public static void Main() { MyEnumerable <int> en = new MyEnumerable <int>(); IEnumerable ee = en; // Duas versões de GetEnumerator diferem apenas no tipo de retorno. // Implementação explícita da interface IEnumerable evita a ambiguidade. en.GetEnumerator(); ee.GetEnumerator(); }
static void Main(string[] args) { IEnumerable <Person> enumerable = new MyEnumerable <Person>(); IEnumerator <Person> enumerator = enumerable.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current.Name); } Console.ReadKey(); }
public void ClassImplementingIEnumerableCastToIEnumerableCanBeEnumerated() { IEnumerable <string> enm = new MyEnumerable(); var e = enm.GetEnumerator(); Assert.True(e.MoveNext()); Assert.AreEqual("x", e.Current); Assert.True(e.MoveNext()); Assert.AreEqual("y", e.Current); Assert.True(e.MoveNext()); Assert.AreEqual("z", e.Current); Assert.False(e.MoveNext()); }
public void ClassImplementingIEnumerableCanBeEnumerated() { MyEnumerable enm = new MyEnumerable(); var e = enm.GetEnumerator(); Assert.IsTrue(e.MoveNext()); Assert.AreEqual(e.Current, "x"); Assert.IsTrue(e.MoveNext()); Assert.AreEqual(e.Current, "y"); Assert.IsTrue(e.MoveNext()); Assert.AreEqual(e.Current, "z"); Assert.IsFalse(e.MoveNext()); }
public void IEnumerable_GetEnumerator() { // Arrange var sequence = new MyEnumerable(); // Act var enumerator = sequence.GetEnumerator(); // Assert var list = new List <char>(); while (enumerator.MoveNext()) { list.Add((char)enumerator.Current); } Assert.Equal("hello world", list.Aggregate(string.Empty, (c, i) => c + i)); }