C# yield return 流程理解
2015-07-25 00:21
405 查看
代码如下: 在Documents1方法中使用yield return之后, 下次在进入Documents1方法就是从上一次yield return部分执行
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace C数据结构与算法 { class Program { static void Main(string[] args) { List<string> docs1 = new List<string>(); docs1.Add("1"); docs1.Add("2"); docs1.Add("3"); docs1.Add("4"); docs1.Add("5"); foreach (string item in Documents1(docs1)) { Console.WriteLine("获取值: " + item); Thread.Sleep(2000); } Console.ReadKey(); } static IEnumerable Documents1(List<string> docs) { foreach (var item in docs) { if (item != "4") { Console.WriteLine("返回值: " + item); //yield return语句返回集合的一个元素,并移动到下一个元素上 yield return item; Console.WriteLine("用完值: " + item); } } Console.Write(" {0} ", "循环完毕"); } } }
相关文章推荐
- 十七、C# 反射、特性和动态编程
- ok6410 LED/GPIO 控制 C#程序
- [转]改善C#程序的建议4:C#中标准Dispose模式的实现
- C#的方法
- [转]详解C#组件开发的来龙去脉
- C#多播委托
- 十六、C# 常用集合类及构建自定义集合(使用迭代器)
- C#中的委托是什么?
- 十五、C# 使用查询表达式的LINQ
- 十四、C# 支持标准查询运算符的集合接口
- C# PPT Operator
- C# ?? 和 ?: 运算符
- 学习笔记---c#中的get与set学习
- C# 内存管理
- C# 内存管理
- C#:实现接口中定义的事件
- 使用SuspendLayout和ResumeLayout
- c# byte[] 与string转化
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- 在C#中读写INI配置文件