Counting Enumerable : IEnumerable « Data Structure « C# / CSharp Tutorial






using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

        class CountingEnumerable : IEnumerable<int>
        {
            public IEnumerator<int> GetEnumerator()
            {
                return new CountingEnumerator();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }

        class CountingEnumerator : IEnumerator<int>
        {
            int current = -1;

            public bool MoveNext()
            {
                current++;
                return current < 10;
            }

            public int Current
            {
                get { return current; }
            }

            object IEnumerator.Current
            {
                get { return Current; }
            }

            public void Reset()
            {
                throw new NotSupportedException();
            }

            public void Dispose()
            {
            }
        }
    class MainClass
    {


        static void Main()
        {
            CountingEnumerable counter = new CountingEnumerable();
            foreach (int x in counter)
            {
                Console.WriteLine(x);
            }
        }
    }








11.43.IEnumerable
11.43.1.Implement IEnumerable with 'yield'
11.43.2.Implement IEnumerable with loop
11.43.3.Provide different IEnumerable implemetation for one class
11.43.4.yield value for IEnumerable
11.43.5.Get list of integer whose value is higher than 10
11.43.6.Iterator Block Iteration Sample
11.43.7.Counting Enumerable