yield(C# 参考)
在块中用于向枚举数对象提供值或发出迭代结束信号。它的形式为下列之一:
class Program { public static IEnumerable Power(int number, int exponent) { int counter = 0; int result = 1; while (counter++ < exponent) { result = result * number; yield return result; } } static void Main() { //HelloCollection helloCollection = new HelloCollection(); //foreach (string s in helloCollection) //{ // Console.WriteLine(s); // Console.ReadLine(); //} MusicTitles titles = new MusicTitles(); foreach (string title in titles) { Console.WriteLine(title); } Console.WriteLine(); foreach (string title in titles.Reverse()) { Console.WriteLine(title); } Console.WriteLine(); foreach (string title in titles.Subset(2, 2)) { Console.WriteLine(title); Console.ReadLine(); } } } public class HelloCollection { public IEnumeratorGetEnumerator() { // yield return语句返回集合的一个元素,并移动到下一个元素上;yield break可以停止迭代 yield return "Hello"; yield return "World"; } } public class MusicTitles { string[] names = { "a", "b", "c", "d" }; public IEnumerator GetEnumerator() { for (int i = 0; i < 4; i++) { yield return names[i]; } } public IEnumerable Reverse() { for (int i = 3; i >= 0; i--) { yield return names[i]; } } public IEnumerable Subset(int index, int length) { for (int i = index; i < index + length; i++) { yield return names[i]; } } }
老实说我理解了啥呢,我只总觉得他可以在循环中,就是所谓的迭代器中返回值的形式,或者终止循环,不必按照规定返回集合类型
刚又看到一篇文章觉得有点茅塞顿开的感觉了
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; } }}