C# 中的"yield"与 "yield break"使用
yield是C#为了简化遍历操作实现的语法糖,我们知道如果要要某个类型支持遍历就必须要实现系统接口IEnumerable,这个接口后续实现比较繁琐要写一大堆代码才能支持真正的遍历功能。举例说明
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text; namespace
{
class Program
{
static void Main(string[] args)
{
HelloCollection helloCollection = new HelloCollection();
foreach (string s in helloCollection)
{
Console.WriteLine(s);
} Console.ReadKey();
}
} //public class HelloCollection : IEnumerable
//{
// public IEnumerator GetEnumerator()
// {
// yield return "Hello";
// yield return "World";
// }
//} public class HelloCollection : IEnumerable
{
public IEnumerator GetEnumerator()
{
Enumerator enumerator = new Enumerator();
return enumerator;
} public class Enumerator : IEnumerator, IDisposable
{
private int state;
private object current; public Enumerator(int state)
{
this.state = state;
} public bool MoveNext()
{
switch (state)
{
case :
current = "Hello";
state = ;
return true;
case :
current = "World";
state = ;
return true;
case :
break;
}
return false;
} public void Reset()
{
throw new NotSupportedException();
} public object Current
{
get { return current; }
} public void Dispose()
{
}
}
}
}
上面注释的部分引用了"yield return”,其功能相当于下面所有代码!可以看到如果不适用yield需要些很多代码来支持遍历操作。
yield return 表示在迭代中下一个迭代时返回的数据,除此之外还有yield break, 其表示跳出迭代,为了理解二者的区别我们看下面的例子
class A : IEnumerable
{
private int[] array = new int[]; public IEnumerator GetEnumerator()
{
for (int i = ; i < ; i++)
{
yield return array[i];
}
}
}
如果你只想让用户访问ARRAY的前8个数据,则可做如下修改.这时将会用到yield break,修改函数如下
public IEnumerator GetEnumerator()
{
for (int i = ; i < ; i++)
{
if (i < )
{
yield return array[i];
}
else
{
yield break;
}
}
}
这样,则只会返回前8个数据.
C# 中的"yield"与 "yield break"使用的更多相关文章
- C#中的foreach和yield
1. foreach C#编译器会把foreach语句转换为IEnumerable接口的方法和属性. foreach (Person p in persons) { Console.WriteLine ...
- .net 反射访问私有变量和私有方法 如何创建C# Closure ? C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密 C#中的foreach和yield 数组为什么可以使用linq查询 C#中的 具名参数 和 可选参数 显示实现接口 异步CTP(Async CTP)为什么那样工作? C#多线程基础,适合新手了解 C#加快Bitmap的访问速度 C#实现对图片文件的压
以下为本次实践代码: using System; using System.Collections.Generic; using System.ComponentModel; using System ...
- 【吐血推荐】简要分析unity3d中剪不断理还乱的yield
在学习unity3d的时候很容易看到下面这个例子: void Start () { StartCoroutine(Destroy()); } IEnumerator Destroy(){ yield ...
- Unity 3D中不得不说的yield协程与消息传递
1. 协程 在Unity 3D中,我们刚开始写脚本的时候肯定会遇到类似下面这样的需求:每隔3秒发射一个烟花.怪物死亡后20秒再复活之类的.刚开始的时候喜欢把这些东西都塞到Update里面去,就像下面这 ...
- 【Unity3D】简要分析unity3d中剪不断理还乱的yield
在学习unity3d的时候很容易看到下面这个例子: void Start () { StartCoroutine(Destroy()); } IEnumerator Destroy(){ yield ...
- 【转】简要分析unity3d中剪不断理还乱的yield
在学习unity3d的时候很容易看到下面这个例子: 1 void Start () { 2 StartCoroutine(Destroy()); 3 } 4 5 IEnumerator Destroy ...
- 简要分析unity3d中剪不断理还乱的yield
在学习unity3d的时候非常easy看到以下这个样例: void Start () { StartCoroutine(Destroy()); } IEnumerator Destroy(){ yie ...
- 从yield 到yield from再到python协程
yield 关键字 def fib(): a, b = 0, 1 while 1: yield b a, b = b, a+b yield 是在:PEP 255 -- Simple Generator ...
- python协程--yield和yield from
字典为动词“to yield”给出了两个释义:产出和让步.对于 Python 生成器中的 yield 来说,这两个含义都成立.yield item 这行代码会产出一个值,提供给 next(...) 的 ...
- 从yield到yield from再到python协程
yield 关键字 def fib(): a,b = 0,1 while 1: yield b a,b = b,a+b yield是在:PEP 255 -- Simple Generators 这个p ...
随机推荐
- chattr命令的用法
chattr (配置文件隐藏属性)[root@www ~]# chattr [+-=][ASacdistu] 文件或目录名称选项与参数:+ :添加某一个特殊参数,其他原本存在参数则不动.- :移除某一 ...
- BZOJ 1026 windy数【数位DP】
1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 10142 Solved: 4712[Submit][St ...
- Codeforces Round #274 (Div. 2) Riding in a Lift(DP 前缀和)
Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 设计模式-状态模式(State Pattern)
本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 状态模式简介 状态模式允许一个对象在其内部状态改变的时候改变它的行为,他的内部会存着好几种状态, ...
- JQuery总结+实例
JQuery是什么? Jquery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari ...
- VB查询数据库之写入数据库——机房收费系统总结(三)
在机房收费系统中,新注册的用户,更改的密码,上机下级记录,上机收费记录等等都要写入数据库,这样,后面的查询才能生效.像数据库中写入数据,首先,找到你要写入数据的数据库中的表,在表中建立新的行,让后再把 ...
- 【BZOJ 2124】【CodeVS 1283】等差子序列
http://www.lydsy.com/JudgeOnline/problem.php?id=2124 http://codevs.cn/problem/1283/ 重点是把判断是否存在3个数组成等 ...
- CSS 笔记——定位尺寸
3. 定位尺寸 -> 尺寸 (1)height 基本语法 height : auto | length 语法取值 auto : 默认值.无特殊定位,根据HTML定位规则分配 length : 由 ...
- 【数据结构】Not so Mobile (6-9)
[UVA839]Not so Mobile 算法入门经典第6章6-9(P157) 题目大意:输入一个树状天平,根据力矩相等原则判断是否平衡. 试题分析:貌似没有什么难点…… #include<i ...
- Java学习笔记(13)
StringBuffer 增加 append(boolean b) 可以添加任意类型的数据到容器中 insert(int offset,boolean b) 指定插入的索引值,插入对应的内容 ...