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 ...
随机推荐
- java学习之Java中JDK,JRE和JVM之间的关系(转载)
最近要重新抓一下java,大量扫技术文档,保存下来供自己查阅.以下转载自http://www.cnblogs.com/xiaofeixiang/p/4085159.html 初学JAVA很容易被其中的 ...
- 导入时如何定制spring-boot依赖项的版本
spring-boot通过maven的依赖管理为我们写好了很多依赖项及其版本,我们可拿来使用.spring-boot文档介绍了两种使用方法,一是继承,二是导入. 通过<parent>继承: ...
- Plus One 解答
Question Given a non-negative number represented as an array of digits, plus one to the number. The ...
- House Robber II 解答
Question After robbing those houses on that street, the thief has found himself a new place for his ...
- tomcat https 未测试成功的版本
- iOS 7 改变Status Bar 颜色
Set the UIViewControllerBasedStatusBarAppearance to NO in the Info.plist. In ViewDidLoad method or a ...
- PHP 超强过滤函数
PHP 超强过滤函数 你有每次要过滤的时候总是去翻曾经的过滤代码的时候么? 你有搜索过怎样防过滤,防攻击的PHP解决方法么? 你有对全然遵循'过滤输入,避免输出',Web界经典说辞么? 事实上 ...
- .net如何后台批量删除
button_Click(Sender sender,Event e){foreach (DataListItem item in DataList1.Items){CheckBox cbox=(Ch ...
- jsp页面使用jstl标签格式化String类型日期
1.引入jstl <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> ...
- 被「李笑来老师」拉黑之「JavaScript微博自动转发的脚本」
故事的背景如下图,李笑来 老师于10月19日在 知乎Live 开设 一小时建立终生受用的阅读操作系统 的讲座,他老人家看到大家伙报名踊跃,便在微博上发起了一个 猜数量赢取iPhone7 的活动. 因为 ...