yield是C#为了简化遍历操作实现的语法糖,我们知道如果要要某个类型支持遍历就必须要实现系统接口IEnumerable,这个接口后续实现比较繁琐要写一大堆代码才能支持真正的遍历功能.举例说明 using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; namespace { class Program { static void M
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)