C#中的yield关键字详解

作者:Darren 时间:2023-01-11 08:19:01 

在"C#中,什么时候用yield return"中,我们了解到:使用yield return返回集合,不是一次性加载到内存中,而是客户端每调用一次就返回一个集合元素,是一种"按需供给"。本篇来重温yield return的用法,探秘yield背后的故事并自定义一个能达到yield return相同效果的类,最后体验yield break的用法。

回顾yield return的用法

以下代码创建一个集合并遍历集合。

class Program
   {
       static Random r = new Random();
       static IEnumerable<int> GetList(int count)
       {
           List<int> list = new List<int>();
           for (int i = 0; i < count; i++)
           {
               list.Add(r.Next(10));
           }
           return list;
       }
       static void Main(string[] args)
       {
           foreach(int item in GetList(5))
               Console.WriteLine(item);
           Console.ReadKey();
       }
   }

使用yield return也能获得同样的结果。修改GetList方法为:

static IEnumerable<int> GetList(int count)
       {
           for (int i = 0; i < count; i++)
           {
               yield return r.Next(10);
           }
       }

通过断点调试发现:客户端每显示一个集合中的元素,都会到GetList方法去获取集合元素。

探密yield

使用yield return获取集合,并遍历。

class Program
   {
       public static Random r = new Random();
       static IEnumerable<int> GetList(int count)
       {
           for (int i = 0; i < count; i++)
           {
               yield return r.Next(10);
           }
       }
       static void Main(string[] args)
       {
           foreach(int item in GetList(5))
               Console.WriteLine(item);
           Console.ReadKey();
       }
   }

生成项目,并用Reflector反编译可执行文件。在.NET 1.0版本下查看GetList方法,发现该方法返回的是一个GetList类的实例。原来yield return是"语法糖",其本质是生成了一个GetList的实例。

C#中的yield关键字详解

那GetList实例是什么呢?点击Reflector中<GetList>链接查看。

C#中的yield关键字详解

  • 原来GetList类实现了IEnumerable和IEnumerator的泛型、非泛型接口

  • yield return返回的集合之所以能被迭代、遍历,是因为GetList内部有迭代器

  • yield return之所以能实现"按需供给",是因为GetList内部有一个_state字段记录这上次的状态

接下来,就模拟GetList,我们自定义一个GetRandomNumbersClass类,使之能达到yield return相同的效果。

using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication2
{
   class Program
   {
       public static Random r = new Random();
       static IEnumerable<int> GetList(int count)
       {
           GetRandomNumbersClass ret = new GetRandomNumbersClass();
           ret.count = count;
           return ret;
       }
       static void Main(string[] args)
       {
           foreach(int item in GetList(5))
               Console.WriteLine(item);
           Console.ReadKey();
       }
   }
   class GetRandomNumbersClass : IEnumerable<int>, IEnumerator<int>
   {
       public int count;//集合元素的数量
       public int i; //当前指针
       private int current;//存储当前值
       private int state;//保存遍历的状态
       #region 实现IEnumerator接口
       public int Current
       {
           get { return current; }
       }
       public bool MoveNext()
       {
           switch (state)
           {
               case 0: //即为初始默认值
                   i = 0;//把指针调向0
                   goto case 1;
                   break;
               case 1:
                   state = 1;//先设置原状态
                   if (!(i < count))//如果指针大于等于当前集合元素数量
                   {
                       return false;
                   }
                   current = Program.r.Next(10);
                   state = 2; //再设置当前状态
                   return true;
                   break;
               case 2: //再次遍历如果state值为2
                   i++;//指针再移动一位
                   goto  case 1;
                   break;

}
           return false;
       }
       //被显式调用的属性
       object IEnumerator.Current
       {
           get { return Current; }
       }
       public void Reset()
       {
           throw new NotImplementedException();
       }
       public void Dispose()
       {
       }
       #endregion
       #region 实现IEnumerable的泛型和非泛型
       public IEnumerator<int> GetEnumerator()
       {
           return this;
       }
       //被显式调用的属性
       IEnumerator IEnumerable.GetEnumerator()
       {
           return GetEnumerator();
       }
       #endregion
   }
}

关于GetRandomNumbersClass类:

  • count表示集合的长度,可以在客户端赋值。当调用迭代器的MoveNext方法,需要把count和当前位置比较,以决定是否可以再向前移动。

  • 字段i相当于索引,指针每次移动一位,i需要自增1

  • current表示当前存储的值,外部通过IEnumerator.Current属性访问

迭代器的MoveNext方法是关键:

  • state字段是整型,表示产生集合过程中的3种状态

  • 当state为0的时候,说明是初始状态,把索引位置调到0,并跳转到state为1的部分

  • 当state为1的时候,首先把状态设置为1,然后判断索引的位置有没有大于或等于集合的长度,接着产生集合元素,把state设置为2,并最终返回true

  • 当sate为2的时候,也就是迭代器向前移动一位,再次执行MonveNext方法的时候,跳转到state为2的语句块部分,把索引位置自增1,再跳转到state为1的语句块中,产生新的集合元素

  • 如此循环

yield break的用法  

假设在一个无限循环的环境中获取一个int类型的集合,在客户端通过某个条件来终止循环。

class Program
   {
       static Random rand = new Random();
       static IEnumerable<int> GetList()
       {
           while (true)
           {
               yield return rand.Next(100);
           }
       }
       static void Main(string[] args)
       {
           foreach (int item in GetList())
           {
               if (item%10 == 0)
               {
                   break;
               }
               Console.WriteLine(item);

}
           Console.ReadKey();
       }
   }

以上,当集合元素可以被10整除的时候,就终止循环。终止循环的时机是在循环遍历的时候。

如果用yield break,就可以在获取集合的时候,当符合某种条件就终止获取集合。

class Program
   {
       static Random rand = new Random();
       static IEnumerable<int> GetList()
       {
           while (true)
           {
               int temp = rand.Next(100);
               if (temp%10 == 0)
               {
                   yield break;
               }
               yield return temp;
           }
       }
       static void Main(string[] args)
       {
           foreach (int item in GetList())
           {
               Console.WriteLine(item);            
           }
           Console.ReadKey();
       }
   }

总结:

  • yield return能返回一个"按需供给"的集合

  • yield return是"语法糖",其背后是一个实现了IEnuerable,IEnumerator泛型、非泛型接口的类,该类维护着一个状态字段,以保证yield return产生的集合能"按需供给"

  • yield break配合yield return使用,当产生集合达到某种条件的时候使用yield break,以终止继续创建集合

来源:https://www.cnblogs.com/darrenji/p/3979048.html

标签:C#,yield,关键字
0
投稿

猜你喜欢

  • C#实现快速排序算法

    2023-03-20 08:41:51
  • 基于Spring Security的Oauth2授权实现方法

    2022-12-19 16:52:18
  • C# ManualResetEvent使用方法详解

    2022-05-15 23:57:51
  • Mybatis 缓存原理及失效情况解析

    2022-12-04 07:28:43
  • 安卓(Android)ListView 显示图片文字

    2023-12-26 12:44:43
  • Unity实现主角移动与摄像机跟随

    2023-09-14 09:32:05
  • Java监听器ActionListener与MouseListener的执行顺序说明

    2022-02-04 20:08:23
  • springboot bean循环依赖实现以及源码分析

    2022-06-05 11:50:15
  • 深入浅析Android坐标系统

    2021-12-15 05:26:34
  • java中利用List的subList方法实现对List分页(简单易学)

    2022-06-18 23:33:09
  • Android在线更新SDK的方法(使用国内镜像)

    2022-08-31 02:40:31
  • java 对象参数去空格方式代码实例

    2023-11-27 09:49:34
  • Java剑指offer之删除链表的节点

    2023-05-19 15:30:27
  • springboot+log4j.yml配置日志文件的方法

    2023-08-07 11:33:17
  • C#泛型的使用及示例详解

    2022-03-14 07:01:59
  • 十分钟理解Java中的动态代理

    2022-05-16 04:53:19
  • Android 控件GridView使用案例讲解

    2023-07-14 17:33:28
  • 微服务间调用Retrofit在Spring Cloud Alibaba中的使用

    2022-09-29 23:13:42
  • Eclipse项目怎么导入IDEA并运行(超详细)

    2021-11-15 06:51:41
  • C#中委托、事件和回调的使用及说明

    2023-08-13 01:00:24
  • asp之家 软件编程 m.aspxhome.com