示例#1
0
    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();
    }
示例#3
0
        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());
        }
示例#4
0
        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());
        }
示例#5
0
        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));
        }