Iterator Block Iteration Sample : IEnumerable « Data Structure « C# / CSharp Tutorial






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

    class IteratorBlockIterationSample : IEnumerable
    {
        object[] values;
        int startingPoint;

        public IteratorBlockIterationSample(object[] values, int startingPoint)
        {
            this.values = values;
            this.startingPoint = startingPoint;
        }

        public IEnumerator GetEnumerator()
        {
            for (int index = 0; index < values.Length; index++)
            {
                yield return values[(index + startingPoint) % values.Length];
            }
        }

        static void Main()
        {
            object[] values = { "a", "b", "c", "d", "e" };
            IteratorBlockIterationSample collection = new IteratorBlockIterationSample(values, 3);
            foreach (object x in collection)
            {
                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