这个函数在处理循环时可以每生成一个数据就返回一个数据让主函数进行处理:
static void Main(string[] args) { foreach (var item in GetNumbers()) Console.WriteLine("Main process. item = " + item); } static IEnumerable<int> GetNumbers() { // 以[0, 1, 2] 初始化数列 list Console.WriteLine("Initializating..."); List<int> list = new List<int>(); for (int i = 0; i < 3; i++) list.Add(i); // 每次 yield return 返回一个list的数据 Console.WriteLine("Processing..."); for (int i = 0; i < list.Count; i++) { Console.WriteLine("Yield called."); yield return list[i]; } Console.WriteLine("Done."); }
Initializating...
Processing...
Yield called.
Main process. item = 0
Yield called.
Main process. item = 1
Yield called.
Main process. item = 2
Done.