yield语句
public class HelloCollection
{
public IEnumerator<string> GetEnumerator()
{
yield return "Hello";
yield return "World";
}
}
static void Main(string[] args)
{
HelloCollection collection = new HelloCollection();
foreach (string s in collection)
Console.WriteLine(s); }
public class HelloCollection
{
public IEnumerator GetEnumerator()
{
return new Enumerator();
}
public class Enumerator : IEnumerator<string>, IEnumerator, IDisposable
{
private int state;
private string current; public Enumerator(int state) { this.state = state; }
bool System.Collections.IEnumerator.MoveNext()
{
switch (state)
{
case :
current = "Hello";
state = ;
return true;
case :
current = "World";
state = ;
return true;
case :
break;
}
return false;
}
void System.Collections.IEnumerator.Reset()
{
throw new NotSupportedException();
}
string System.Collections.Generic.IEnumerator<string>.Current
{
get { return current; }
}
object System.Collections.IEnumerator.Current
{
get { return current; }
}
void IDisposable.Dispose() { }
}
}
public class MusicTitles
{
string[] names = { "Tubular Bells", "Hergest Ridge", "Ommadawn", "Platinum" }; public IEnumerator<string> GetEnumerator()
{
for (int i = ; i < ; i++)
yield return names[i];
}
public IEnumerable<string> Reverse()
{
for (int i = ; i >= ; i--)
yield return names[i];
}
public IEnumerable<string> Subset(int index, int length)
{
for (int i = index; i < index + length; i++)
yield return names[i];
}
}
public class GameMoves
{
private IEnumerator cross;
private IEnumerator circle; public GameMoves()
{
cross = Cross();
circle = Circle();
}
private int move = ;
const int MaxMoves = ; public IEnumerator Cross()
{
while (true)
{
Console.WriteLine("Cross, move {0}", move);
if (++move >= MaxMoves)
yield break;
yield return circle;
}
}
public IEnumerator Circle()
{
while (true)
{
Console.WriteLine("Circle, move {0}", move);
if (++move >= MaxMoves) yield break;
yield return cross;
}
}
}
var game = new GameMoves();
IEnumerator enumerator = game.Cross();
while (enumerator.MoveNext())
enumerator = enumerator.Current as IEnumerator;
yield语句的更多相关文章
- Python生成器以及yield语句
生成器是一种暂缓求值的技术,它可以用来生成一系列的值,但不会一次性生成所有的值,而只在需要的时候才计算和生成一个值. 通过yield语句构建生成器 要得到一个生成器,我们需要定义一个函数,这个函数返回 ...
- javascript笔记04:let语句 和 yield语句 和 with语句
1.yield语句: <script type="application/javascript; version=1.7"> function generator() ...
- 生成器以及yield语句
生成器以及yield语句最初的引入是为了让程序员可以更简单的编写用来产生值的序列的代码. 以前,要实现类似随机数生成器的东西,需要实现一个类或者一个模块,在生成数据的同时 保持对每次调用之间状态的跟踪 ...
- Python with yield语句
1.with 语句 语法: with expression as variable 需要引入一个上下文管理协议,实现的方法是为一个类定义 __enter__() 和 __exit__() 方法, 在执 ...
- python -迭代器与生成器 以及 iterable(可迭代对象)、yield语句
我刚开始学习编程没多久,对于很多知识还完全不知道,而有些知道的也是一知半解,我想把学习到的知识记录下来,一是弥补记忆力差的毛病,二也是为了待以后知识能进一步理解透彻时再回来做一个补充. 参考链接: 完 ...
- C#:foreach语句,yield语句
原文:C#:foreach语句,yield语句 1. foreach语句 C#编译器会把foreach语句转换为IEnumerable接口的方法和属性. foreach (Person p in pe ...
- Python中生成器和yield语句的用法详解
Python中生成器和yield语句的用法详解 在开始课程之前,我要求学生们填写一份调查表,这个调查表反映了它们对Python中一些概念的理解情况.一些话题("if/else控制流" ...
- python 生成器 yield语句
生成器就是一个返回迭代器(iterator)的函数. 包含了 yield 的函数,就是一个生成器. 生成器每使用yield语句产生一个值,函数就会被冻结(暂停执行),被唤醒后(即再次调用)接着上次执行 ...
- 6.2 C# 2:利用 yield 语句简化迭代器
class Program { static void Main(string[] args) { object[] values = new object[] { "a", &q ...
随机推荐
- 【Android病毒分析报告】 - ZooTiger “集恶意推广、隐私窃取、恶意吸费于一体”
本文章由Jack_Jia编写,转载请注明出处. 文章链接:http://blog.csdn.net/jiazhijun/article/details/11772379 作者:Jack_Jia ...
- Linux系统维护修复模式
基于PXE方式的Linux系统维护工具箱 在安装RedHat Linux系统的过程中,我们知道可以通过PXE方式进行安装,从而解决了无光驱或无安装介质(光盘)来安装操作系统.但是当系统由于某种 ...
- Unique Paths II 解答
Question Follow up for "Unique Paths": Now consider if some obstacles are added to the gri ...
- LeeCode-Pow(x, n)
Implement pow(x, n). double myPow(double x, int n) { ) return 1.0; ) return 1.0/pow(x,-n); ); }
- wget命令3(转载)
Wget下载终极用法和15个详细的例子 Wget是一种很好用的因特网下载工具,他具有的很多特性是其他工具所不能比拟的,再者他是一个轻量级可配置的下载工具. 本文结合例子介绍Windows下wget的多 ...
- svn 清理失败 (cleanup 失败) 的解决方法
svn 清理失败 (clean up 失败) 的解决方法 參考:http://www.tuicool.com/articles/biy6na 解决方法: step1: 到 sqlite官网 (http ...
- AIX-vi操作-提示Unknown terminal type的问题解决方法
AIX-vi操作-提示Unknown terminal type的问题解决方法AIX Version 5.3$ vi /etc/profilelinux: Unknown terminal type[ ...
- python 笔记1--基础类型
list 操作 append() 添加最外面 insert(pos,content) 插入指定地方 pop() 删除最外面 pop(pos) 删除指定地方 list中可以有list,且能用二维数组的方 ...
- 快速解决js开发下拉框中blur与click冲突
在开发中我们会经常遇到blur和click冲突的情况.下面叙述了开发中常遇到的"下拉框"的问题,并提供了两种解决方案. 一.blur和click事件简述 blur事件:当元素失去焦 ...
- WebView高危接口安全检测
高危]WebView高危接口安全检测共2处详细内容:在Android系统4.3.1~3.0版本,系统webview默认添加了searchBoxJavaBridge_接口,如果未移除该接口可能导致低版本 ...