【C#学习】yield关键字

协程 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个数据.

C#语法中有个特别的关键字yield, 它是干什么用的呢?

来看看专业的解释:

yield 是在迭代器块中用于向枚举数对象提供值或发出迭代结束信号。它的形式为下列之一:
yield return <expression>;
yield break

 

看如下例子:

 

复制代码
 1  1     public class CustomCollection :IEnumerable {
 2  2         
 3  3         public static void Main (string[] args)
 4  4         {
 5  5             CustomCollection cc = new CustomCollection ();
 6  6 
 7  7             foreach (String word in cc) {
 8  8                 Console.WriteLine ("word:" +word);
 9  9             }
10 10         }
11 11 
12 12         public IEnumerator GetEnumerator(){
13 13 
14 14             yield return "Hello";
15 15             yield return "Boys";
16 16             yield return "And";
17 17             yield return "Girls";
18 18             //return new HelloBoyGirls();
19 19 
20 20         }
21 21     }
22 22 
23 23 //    public class HelloBoyGirls: IEnumerator {
24 24 //        private int cusor = -1;
25 25 //        private String[] words = {"Hello", "Boys", "And", "Girls"};
26 26 //        
27 27 //        public bool MoveNext ()
28 28 //        {
29 29 //            cusor++;
30 30 //            return cusor < words.Length;
31 31 //        }
32 32 //
33 33 //        public void Reset ()
34 34 //        {
35 35 //            cusor = 0;
36 36 //        }
37 37 //
38 38 //        public object Current {
39 39 //            get {
40 40 //                return words [cusor];
41 41 //            }
42 42 //        }
43 43 //    }
复制代码

 

 

 

上面的例子是实现了一个自定义的迭代器;实现可迭代(可以用foreach)的数据集合,必须实现GetEmumerator()方法,返回实现了IEmumerator的对象实例。

完成这个, 有两种方法,一种是用上面注释掉的代码,一种是用yield return. yield return 需要配合IEmumerator进行使用, 在外部foreach循环中,它会执行GetEmumerator()方法,遇到yield return, 做了如下两件事情:

1.记录下当前执行到的代码位置

2. 将代码控制权返回到外部, yield return 后面的值, 作为迭代的当前值。

当执行下一个循环, 从刚才记录的代码位置后面, 开始继续执行代码。

简单地说, yield return 就是实现IEmumerator的超级简化版, 是不是很简单?

 

那么问题又来了, yield return 是如何决定循环该结束,yield return 之后的代码, 什么时候执行呢?

把上面的例子改造一下, 不要用方便的foreach了, 用while 循环自己控制:

复制代码
 1     public class CustomCollection :IEnumerable {
 2         
 3         public static void Main (string[] args)
 4         {
 5             CustomCollection cc = new CustomCollection ();
 6 
 7             IEnumerator enumerator = cc.GetEnumerator ();
 8             while (true) {
 9                 bool canMoveNext = enumerator.MoveNext ();
10                 Console.WriteLine ("canMoveNext:" +canMoveNext);
11                 if (!canMoveNext)
12                     break;
13                 Object obj = enumerator.Current;
14                 Console.WriteLine ("current obj:" +obj);
15             }
16 //            foreach (String word in cc) {
17 //                Console.WriteLine ("word:" +word);
18 //            }
19             Console.WriteLine ("Main End.");
20 
21         }
22 
23         public IEnumerator GetEnumerator(){
24 
25             yield return "Hello";
26             yield return "Boys";
27             yield return "And";
28             yield return "Girls";
29 
30             Console.WriteLine ("After all yield returns.");
31             //return new HelloBoyGirls();
32 
33         }
34     }
复制代码

 

运行代码, 结果是:

复制代码
canMoveNext:True
current obj:Hello
canMoveNext:True
current obj:Boys
canMoveNext:True
current obj:And
canMoveNext:True
current obj:Girls
After all yield returns.
canMoveNext:False
Main End.
复制代码

说明, 在GetEmumerator()中, 只有yield return 语句, 外部调用MoveNext()都为true, current就是yield return后面的对象

除了yield return, 还有yield break; yield break 的作用是, 停止循环, MoveNext()为false, yield break 之后的语句, 不会被执行!

有兴趣的童鞋, 可以自己写个例子试试。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值