简单例子:
public static System.Collections.Generic .IEnumerable<int> Power(int number, int exponent) { int result = 1; for (int i = 0; i < exponent; i++) { result = result * number; yield return result; } }
var abc = Power(2, 8); foreach (int i in Power(2, 8)) { Console.Write("{0} ", i); }
abc,输出结果是
2 4 8 16 32 64 128 256
http://msdn.microsoft.com/zh-cn/library/9k7k7cf0.aspx
http://www.cnblogs.com/ldp615/archive/2011/10/07/can-you-find-the-error-in-foreach-extension-method.html
时间: 2024-10-08 07:26:09