关键字yiel的使用方法
在迭代器块中用于向枚举数对象提供值或发出迭代结束信号。它的形式为下列之一:
Code
yield return <expression>;
yield break;
using System;
using System.Collections;
public class List
{
public static IEnumerable Power(int number, int exponent)
{
int counter = 0;
int result = 1;
while (counter++ < exponent)
{
result = result * number;
yield return result;
}
}
static void Main()
{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2, 8))
{
Console.Write("{0} ", i);
}
}
}