public class List{ //using System.Collections; 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); } }}/*Output:2 4 8 16 32 64 128 256 */ [解决办法] 当然 .NET 2.0 以后 使用Ilist 反省要好多了
C# code
static IEnumerable<int> WithYield() { for (int i = 0; i < 20; i++) { Console.WriteLine(i.ToString()); if(i > 2) yield return i; } } [解决办法]