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 ...
随机推荐
- 使用 asp.net Web API 2的坑
使用工具: Googl 浏览器+PostMan 插件 写了个 控制器 添加了个Action,结果呢?GET 方式请求没问题. POST一直,在服务器端获取不了参数...找了官方的文档 .各种雨里雾 ...
- 软件测试-nextDate问题
NextDate 函数包含三个变量:month . day 和 year ,函数的输出为输入日期后一天的日期. 例如,输入为 2006年3月 7日,则函数的输出为 2006年3月8日 .要求输入变量 ...
- ZOJ-3410Layton's Escape(优先队列+贪心)
Layton's Escape Time Limit: 2 Seconds Memory Limit: 65536 KB Professor Layton is a renowned arc ...
- 【刷题 Python Tip】题目6~10
[题目6]输出100以内的所有素数,素数之间以一个空格区分 from math import sqrt print ' '.join(str(key) for key in [x for x in x ...
- Unity 编辑器扩展自定义窗体
这次看见Unity还可以自定义弹出窗体,让我很好奇.于是就去网上找文章看了看. 如果想自定义窗体需要把类放入Editor文件夹下面. 代码如下: using UnityEngine; using Un ...
- Servlet页面间对象传递的方法
Servlet页面间对象传递的方法 1.request 2.session 3.application 4.cookie 5.其它的
- c-version:null]] could not deserialize the servlet-context scoped attribute with name: "MENU_LIST"
<Jul 26, 2013 10:45:02 AM CST> <Error> <HTTP> <BEA-101362> <[ServletConte ...
- Maven 工程下 Spring MVC 站点配置 (三) C3P0连接池与@Autowired的应用
Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 前两篇文章主要是对站点和数据库操作配置进行了演示,如果单 ...
- JQ 更改li 颜色
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- oc特有语法
分类 问题 1.什么是分类? 就是把一个类的功能,分出一部分来放在一个独立的文件中 2.分类的语法是什么样的? @interface Person(SuperMan) 3.分类与类是什么关系? 分类依 ...