yield value for IEnumerable : IEnumerable « Data Structure « C# / CSharp Tutorial






using System;
using System.Collections.Generic;

public class MainClass
{
    static public IEnumerable<int> Powers( int from,
                                           int to ) {
        for( int i = from; i <= to; ++i ) {
            yield return (int) Math.Pow( 2, i );
        }
    }

    static void Main() {
        IEnumerable<int> powers = Powers( 0, 16 );
        foreach( int result in powers ) {
            Console.WriteLine( result );
        }
    }
}
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536








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