C# 中的"yield"使用
来源:互联网 发布:超女网络海选冥月 编辑:程序博客网 时间:2024/06/04 18:29
转自:http://www.cnblogs.com/kingcat/archive/2012/07/11/2585943.html
yield是C#为了简化遍历操作实现的语法糖,我们知道如果要要某个类型支持遍历就必须要实现系统接口IEnumerable,这个接口后续实现比较繁琐要写一大堆代码才能支持真正的遍历功能。举例说明
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace
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(0);
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 0:
current = "Hello";
state = 1;
return true;
case 1:
current = "World";
state = 2;
return true;
case 2:
break;
}
return false;
}
public void Reset()
{
throw new NotSupportedException();
}
public object Current
{
get { return current; }
}
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(0);
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 0:
current = "Hello";
state = 1;
return true;
case 1:
current = "World";
state = 2;
return true;
case 2:
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[10];
public IEnumerator GetEnumerator()
{
for (int i = 0; i < 10; i++)
{
yield return array[i];
}
}
}
{
private int[] array = new int[10];
public IEnumerator GetEnumerator()
{
for (int i = 0; i < 10; i++)
{
yield return array[i];
}
}
}
如果你只想让用户访问ARRAY的前8个数据,则可做如下修改.这时将会用到yield break,修改函数如下
public IEnumerator GetEnumerator()
{
for (int i = 0; i < 10; i++)
{
if (i < 8)
{
yield return array[i];
}
else
{
yield break;
}
}
}
{
for (int i = 0; i < 10; i++)
{
if (i < 8)
{
yield return array[i];
}
else
{
yield break;
}
}
}
这样,则只会返回前8个数据.
0 0
- C# 中的"yield"使用
- C#中的yield关键字
- C#中的yield return
- C#中的yield关键字
- c#中的yield关键字
- C#中的yield关键字
- C#中的yield return
- C# yield 关键字 使用
- C# 中yield使用
- C#中的using和yield return混合使用
- C#中的foreach和yield
- C# 使用yield实现循环
- foreach()中的yield的使用
- python中的yield简单使用
- 【C#函数式编程】C#中的函数式编程(三) —— yield关键字的使用
- 用C#中的yield实现Coroutine框架
- C#中的yield关键字的使用方法介绍
- C#使用yield实现高效查询
- Android基础知识之控件系列(2)——Button及自定义背景
- LeakCanary:让内存泄漏无所遁形
- 数独
- [求助][已解决]MFC+OpenCV在子窗体中显示图片 20160312
- POJ 2288 Islands and Bridges(状压dp)
- C# 中的"yield"使用
- Web存储——cookie
- 8086 汇编
- PHP_mySQL_通过php代码+sql查询语句读取数据库信息
- nyoj 三个水杯
- 蓦然回首-面向对象
- ColorStateList实现TextView文字按下时颜色变化
- openwrt 15.05 branch (Chaos Calmer)编译出的固件bootargs被覆盖
- 146 memcache