【发布时间】:2011-05-26 05:49:27
【问题描述】:
好的,当我在构建自定义枚举器时,我注意到了这种与 yield
相关的行为假设你有这样的事情:
public class EnumeratorExample
{
public static IEnumerable<int> GetSource(int startPoint)
{
int[] values = new int[]{1,2,3,4,5,6,7};
Contract.Invariant(startPoint < values.Length);
bool keepSearching = true;
int index = startPoint;
while(keepSearching)
{
yield return values[index];
//The mind reels here
index ++
keepSearching = index < values.Length;
}
}
}
在技术上从函数返回后,是什么让编译器能够在 while 循环中执行索引 ++ 和其余代码?
【问题讨论】:
标签: c# enumeration yield yield-return