yield 有让步、屈服 的意思,可以用
yield break
来终止迭代
一个简单的迭代器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Yield_test
{
class Program
{
static void Main(string[] args)
{
foreach (int i in S(10)) {
Console.WriteLine(i);
}
Console.ReadLine();
}
static IEnumerable<int> S(int a) {
for (int i = 0; i < a; i++) {
yield return i;
}
}
}
}
输出