.Net using the Yield keyword

using System;
using System.Collections;

namespace YieldDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var i in Power(2, 28))
            {
                Console.WriteLine("{0} ", i);
            }
            
            Console.ReadKey();
        }
        
        private static IEnumerable Power(int number, int exponent)
        {
            var counter = 0;
            var result = 1;
            
            while (counter++ < exponent)
            {   
                yield return result = result * number;
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *