C# 迭代器简析
来源:互联网 发布:软件安全性测试 csdn 编辑:程序博客网 时间:2024/06/05 15:56
迭代器(iterator)有时又称游标(cursor)是程序设计的软件设计模式,可在容器(container,例如链表或阵列)上遍访的接口,设计人员无需关心容器的内容。
简单迭代器的实现:
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace ConsoleApplication1{ class Program { static bool delay = true; static void Main(string[] args) { var result = getResult(); Console.WriteLine("first writeline : "); //for循环控制迭代 /* for (var i = result.Current; result.MoveNext(); ) { i = result.Current; Console.WriteLine("outside writeLine : {0}", i); }*/ //手动控制迭代 var i = result.Current; Console.WriteLine("outside writeLine : {0}", i); result.MoveNext(); i = result.Current; Console.WriteLine("outside writeLine : {0}", i); result.MoveNext(); i = result.Current; Console.WriteLine("outside writeLine : {0}", i); result.MoveNext(); i = result.Current; Console.WriteLine("outside writeLine : {0}", i); result.MoveNext(); i = result.Current; Console.WriteLine("outside writeLine : {0}", i); result.MoveNext(); i = result.Current; Console.WriteLine("outside writeLine : {0}", i); result.MoveNext(); i = result.Current; Console.WriteLine("outside writeLine : {0}", i); result.MoveNext(); i = result.Current; Console.WriteLine("outside writeLine : {0}", i); result.MoveNext(); i = result.Current; Console.WriteLine("outside writeLine : {0}", i); Console.ReadLine(); } static IEnumerator getResult() { //var arr = Array.CreateInstance(typeof(Program), 1); var arr = new int[] { 1, 2, 3, 45, 23, 23, 4, 3213 }; foreach (var item in arr) { yield return item; if (delay) Thread.Sleep(100); } } } }
0 0
- C# 迭代器简析
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- c#
- c#
- C#
- C#
- C#
- c#
- 深入分析nutch的抓取流程
- 两个list集合得到linq外连接的效果
- Service的生命周期
- mysql学习笔记(5)
- 利用selenium webdriver下载不同类型的文件(pdf,txt等等)
- C# 迭代器简析
- Java根据用户IP查找用户地址信息
- 关于"LNK2005"错误
- 无效的字段名或字段索引
- 九度oj-1471-合并字符
- 手机安全卫士简单实现原理
- 平方根升余弦滤波器
- dfasdfe
- java.lang.IllegalArgumentException: taglib definition not consistent with specification version