码迷,mamicode.com
首页 > Windows程序 > 详细

C# yield return; yield break;

时间:2018-09-29 18:20:02      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:demo   stat   done   adk   OLE   nbsp   string   --   write   

using System;
using System.Collections;
 
namespace YieldDemo
{
     
    class Program
    {
        public static IEnumerable Power(int num, int exponent)
        {
            int counter = 0;
            int result = 1;
            while (counter++ < exponent)
            {
                //if (counter == 4) yield break;
                if (counter == 4) yield return null;
                result = result * num;
                yield return result;
            }
        }
        public static IEnumerator Power2(int num, int exponent)
        {
            int counter = 0;
            int result = 1;
            while (counter++ < exponent)
            {
                //if (counter == 4) yield break;
                if (counter == 4) yield return null;
                result = result * num;
                yield return result;
            }
        }
        static void Main(string[] args)
        {
            foreach (var item in Power(2,8))
            {
                Console.WriteLine(item);
            }
 
            Console.WriteLine("---");
            var e = Power2(2, 8);
            while (e.MoveNext())
            {
                var i = e.Current;
                Console.WriteLine(i);
            }
 
            Console.WriteLine("main done");
            Console.ReadKey();
        }
    }
}

 

C# yield return; yield break;

标签:demo   stat   done   adk   OLE   nbsp   string   --   write   

原文地址:https://www.cnblogs.com/Cein/p/9725109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!