C# yield return; yield break;

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();
}
}
}

  

上一篇:mac下安装c++开发环境


下一篇:【Java EE 学习 49 上】【Spring学习第一天】【基本配置】