新足迹

 找回密码
 注册

精华好帖回顾

· 粽子糖 (2005-2-3) chris2002 · 五香酱牛肉:) (2005-7-1) 东北大厨
· 啫啫鱼头煲 (2008-11-24) komen · 老大难问题,7座车的选择-----------最后还是买了tarago (2013-11-1) iamwhoami
Advertisement
Advertisement
查看: 1358|回复: 7

yield in C# [复制链接]

头像被屏蔽

禁止发言

发表于 2011-2-3 14:40 |显示全部楼层
此文章由 澳贼 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 澳贼 所有!转贴必须注明作者、出处和本声明,并保持内容完整
yield in C# 到底有啥好处?? 那位大仙给举例说明一下???

http://msdn.microsoft.com/en-us/library/9k7k7cf0(v=vs.80).aspx

没看懂

http://www.ytechie.com/2009/02/u ... nd-performance.html

还是没看懂
签名被屏蔽
Advertisement
Advertisement

2007 年度奖章获得者

发表于 2011-2-3 14:57 |显示全部楼层
此文章由 coolioo 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 coolioo 所有!转贴必须注明作者、出处和本声明,并保持内容完整
Simply put, yield is a syntax sugar

If you want to use foreach on a customise collection class/type, the class has to implement the IEnumerable interface.
IEnumerable interface has one method: GetEnumerator(), which return a IEnumerator (interface). You have to create a class that implement the IEnumerator interface, this class contains the logic for how to iterate through you collection type.

In C# 2.0, a new key word is introduced, “yield return”. It is a syntax sugar which helps you quickly create a iterator method returns a Enumerator. If compiler sees “yield return” in a method, it will know that this isn’t a normal method but one implemented with an iterator block. Then compiler will automatically generate a Enumerator class using the logic. Normally “yield return” is used in the GetEnumerator() method when implementing IEnumerable interface to reduce code. By doing this, you dont need to create another class implement the IEnumerator interface, compiler will do this for you using the logic in the “yield return” method.

You can only use “yield return” in iterator blocks to implement methods that have a return type of IEnumerable, IEnumerator, or one of the generic equivalents.

[ 本帖最后由 coolioo 于 2011-2-3 15:58 编辑 ]
C.B
头像被屏蔽

禁止发言

发表于 2011-2-3 15:02 |显示全部楼层
此文章由 澳贼 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 澳贼 所有!转贴必须注明作者、出处和本声明,并保持内容完整
原帖由 coolioo 于 2011-2-3 15:57 发表
Simply put, yield is a syntax sugar

If you want to use foreach on a customise collection class/type, the class has to implement the IEnumerable interface.
IEnumerable interface has one method: GetEnu ...


还是不太懂
签名被屏蔽

2007 年度奖章获得者

发表于 2011-2-3 15:06 |显示全部楼层
此文章由 coolioo 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 coolioo 所有!转贴必须注明作者、出处和本声明,并保持内容完整
不懂是正常的,C#2.0,3.0增加了很多新的keyword,其实没什么大用,都是Syntax Sugar,就是让你少写几行代码而已。其实这样的代码读起来更费劲。

评分

参与人数 1积分 +4 收起 理由
澳贼 + 4

查看全部评分

发表于 2011-2-3 15:14 |显示全部楼层
此文章由 uowzd01 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 uowzd01 所有!转贴必须注明作者、出处和本声明,并保持内容完整
没用过

2007 年度奖章获得者

发表于 2011-2-3 15:57 |显示全部楼层
此文章由 coolioo 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 coolioo 所有!转贴必须注明作者、出处和本声明,并保持内容完整
yield return主要的作用是简化实现IEnumerable interface的代码

先读这个代码,http://msdn.microsoft.com/en-us/ ... 1%28v=VS.80%29.aspx
People是一个自定义类,为了在foreach中使用People,People必须实现IEnumerable接口。为了实现IEnumerable接口,必须再定义一个新类PeopleEnum来实现IEnumerator接口。

如果用yield return,你就不必定义PeopleEnum类,是代码长度大大缩小。
  1.     public class PeopleNew : IEnumerable
  2.     {
  3.         private Person[] _people;
  4.         public PeopleNew(Person[] pArray)
  5.         {
  6.             _people = new Person[pArray.Length];

  7.             for (int i = 0; i < pArray.Length; i++)
  8.             {
  9.                 _people[i] = pArray[i];
  10.             }
  11.         }

  12.         public IEnumerator GetEnumerator()
  13.         {
  14.             for (int i = 0; i < _people.Length; i++)
  15.             {
  16.                 yield return _people[i];
  17.             }
  18.         }
  19.     }
复制代码

[ 本帖最后由 coolioo 于 2011-2-3 16:59 编辑 ]
C.B
Advertisement
Advertisement

2010年度奖章获得者

发表于 2011-2-3 16:11 |显示全部楼层
此文章由 dalaohu 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 dalaohu 所有!转贴必须注明作者、出处和本声明,并保持内容完整
Without yield, u need to manually add items to collection.

Its a code saver

With one small restriction

Only return ienumerable<t>

wp7 keyboard rox

With one small restriction

Only English...

评分

参与人数 1积分 +2 收起 理由
澳贼 + 2 了解

查看全部评分

足迹 Reader is phenomenal. If you never used, you never lived 火速下载

2007 年度奖章获得者

发表于 2011-2-3 16:15 |显示全部楼层
此文章由 coolioo 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 coolioo 所有!转贴必须注明作者、出处和本声明,并保持内容完整
Only return ienumerable<t>


what does this mean??

发表回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Advertisement
Advertisement
返回顶部