C# 中的"yield"与 "yield break"使用

 

    yield是C#为了简化遍历操作实现的语法糖,我们知道如果要要某个类型支持遍历就必须要实现系统接口IEnumerable,这个接口后续实现比较繁琐要写一大堆代码才能支持真正的遍历功能。举例说明 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Collections;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             HelloCollection helloCollection = new HelloCollection();
14             foreach (string s in helloCollection)
15             {
16                 Console.WriteLine(s);
17             }
18 
19             Console.ReadKey();
20         }
21     }
22 
23     //public class HelloCollection : IEnumerable
24     //{
25     //    public IEnumerator GetEnumerator()
26     //    {
27     //        yield return "Hello";
28     //        yield return "World";
29     //    }
30     //}
31 
32     public class HelloCollection : IEnumerable
33     {
34         public IEnumerator GetEnumerator()
35         {
36             Enumerator enumerator = new Enumerator(0);
37             return enumerator;
38         }
39 
40         public class Enumerator : IEnumerator, IDisposable
41         {
42             private int state;
43             private object current;
44 
45             public Enumerator(int state)
46             {
47                 this.state = state;
48             }
49 
50             public bool MoveNext()
51             {
52                 switch (state)
53                 {
54                     case 0:
55                         current = "Hello";
56                         state = 1;
57                         return true;
58                     case 1:
59                         current = "World";
60                         state = 2;
61                         return true;
62                     case 2:
63                         break;
64                 }
65                 return false;
66             }
67 
68             public void Reset()
69             {
70                 throw new NotSupportedException();
71             }
72 
73             public object Current
74             {
75                 get { return current; }
76             }
77 
78             public void Dispose()
79             {
80             }
81         }
82     }
83 }

   上面注释的部分引用了"yield return”,其功能相当于下面所有代码!可以看到如果不适用yield需要些很多代码来支持遍历操作。

    yield return 表示在迭代中下一个迭代时返回的数据,除此之外还有yield break, 其表示跳出迭代,为了理解二者的区别我们看下面的例子

 1 class A : IEnumerable
 2 {
 3     private int[] array = new int[10];
 4 
 5     public IEnumerator GetEnumerator()
 6     {
 7         for (int i = 0; i < 10; i++)
 8         {
 9             yield return array[i];
10         }
11     }
12 }

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

 1 public IEnumerator GetEnumerator()
 2 {
 3     for (int i = 0; i < 10; i++)
 4     {
 5         if (i < 8)
 6         {
 7             yield return array[i];
 8         }
 9         else
10         {
11             yield break;
12         }
13     }
14 }

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

原文地址:https://www.cnblogs.com/endv/p/7087669.html