惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
博客园_首页
爱范儿
爱范儿
博客园 - 叶小钗
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 【当耐特】
Y
Y Combinator Blog
量子位
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Blog of Author Tim Ferriss
月光博客
月光博客
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans

博客园 - 老金

直接初始化和复制初始化 C++ 内连接与外连接 (转) mysql-Innodb事务隔离级别-repeatable read详解(转) Linux操作系统多线程信号总结 Linux操作系统多线程信号总结(转) 转-C/C++捕获段错误,打印出错的具体位置(精确到哪一行) 关键字restrict简介 使用SMTP 和POP3 协议实现收发邮件(C/C++) (转) C/C++中的日期和时间 time_t与struct tm Unicode(UTF-8, UTF-16)令人混淆的概念 跟着url走一圈(ASP.NET请求底层流转个人总结 二) 跟着url走一圈(ASP.NET请求底层流转个人总结 一) GDB教程详解&打印STL容器 谈谈.Net中的协变和逆变(转) 对象的比较与排序:IComparable和IComparer接口 jsoncpp在linux下的配置 在Ubuntu上安装并C++使用libmemcached C++ Primer 第十六章 模板与范型编程 C++ Primer 第十五章 面向对象编程
C# 中的"yield"使用
老金 · 2012-07-11 · via 博客园 - 老金

    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(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];
        }
    }
}

    如果你只想让用户访问ARRAY的前8个数据,则可做如下修改.这时将会用到yield break,修改函数如下

public IEnumerator GetEnumerator()
{
    for (int i = 0; i < 10; i++)
    {
        if (i < 8)
        {
            yield return array[i];
        }
        else
        {
            yield break;
        }
    }
}

    这样,则只会返回前8个数据.