前言

  前段时间了解到yield关键字,一直觉得还不错。今天给大家分享一下yield关键字的用法。yield return 返回集合不是一次性返回所有集合元素,而是一次调用返回一个元素。具体如何使用yield return 返回集合呢?我们一起往下面看吧。

yield使用介绍

yield return 和yield break:

我们看下平常循环返回集合的使用操作(返回1-100中的偶数):

  class Program
{
static private List<int> _numArray; //用来保存1-100 这100个整数 Program() //构造函数。我们可以通过这个构造函数往待测试集合中存入1-100这100个测试数据
{
_numArray = new List<int>(); //给集合变量开始在堆内存上开内存,并且把内存首地址交给这个_numArray变量 for (int i = ; i <= ; i++)
{
_numArray.Add(i); //把1到100保存在集合当中方便操作
}
} static void Main(string[] args)
{
new Program(); TestMethod(); } //测试求1到100之间的全部偶数
static public void TestMethod()
{
foreach (var item in GetAllEvenNumberOld())
{
Console.WriteLine(item); //输出偶数测试
}
} /// <summary>
/// 使用平常返回集合方法
/// </summary>
/// <returns></returns>
static IEnumerable<int> GetAllEvenNumberOld()
{
var listNum = new List<int>();
foreach (int num in _numArray)
{
if (num % == ) //判断是不是偶数
{
listNum.Add(num); //返回当前偶数 }
}
return listNum;
}
}

然后我们再看看使用yield return返回集合操作:

 class Program
{
static private List<int> _numArray; //用来保存1-100 这100个整数 Program() //构造函数。我们可以通过这个构造函数往待测试集合中存入1-100这100个测试数据
{
_numArray = new List<int>(); //给集合变量开始在堆内存上开内存,并且把内存首地址交给这个_numArray变量 for (int i = ; i <= ; i++)
{
_numArray.Add(i); //把1到100保存在集合当中方便操作
}
} static void Main(string[] args)
{
new Program(); TestMethod(); } //测试求1到100之间的全部偶数
static public void TestMethod()
{
foreach (var item in GetAllEvenNumber())
{
Console.WriteLine(item); //输出偶数测试
}
} //使用Yield Return情况下的方法
static IEnumerable<int> GetAllEvenNumber()
{ foreach (int num in _numArray)
{
if (num % == ) //判断是不是偶数
{
yield return num; //返回当前偶数 }
}
yield break; //当前集合已经遍历完毕,我们就跳出当前函数,其实你不加也可以
//这个作用就是提前结束当前函数,就是说这个函数运行完毕了。
} }

与平常return比较

上面我们看到了yield return 的使用方法,那么这个与return返回集合有什么区别呢?我们看下面一个案例来进行分析:

我们首先先看通过returun返回集合的一个案例:

    class Program
{
static void Main(string[] args)
{
foreach (var item in GetNums())
{
Console.WriteLine($" common return:{item}");
}
} /// <summary>
/// 平常return 返回集合
/// </summary>
/// <returns></returns>
public static IEnumerable<int> GetNums()
{
var listNum = new List<int>();
for (int i = ; i < ; i++)
{
Console.WriteLine($"yield return:{i}");
listNum.Add(i);
}
return listNum;
} }

通过代码的运行结果,我们可以看到这里返回的结果 yield return 和comment return是分成两边的。先执行完一个然后开始执行另外一个。不干涉。

我们接着看下使用yield return返回集合:

    class Program
{
static void Main(string[] args)
{
foreach (var item in GetNumsYield())
{
Console.WriteLine($" common return:{item}");
}
} /// <summary>
/// 通过yield return 返回集合
/// </summary>
/// <returns></returns>
public static IEnumerable<int> GetNumsYield()
{
for (int i = ; i < ; i++)
{
Console.WriteLine($"yield return:{i}");
yield return i;
}
}
}

我们看这个运行结果,这里yield return 和comment return 的输出完全交替了。这里说明是一次调用就返回了一个元素。

通过上面的案例我们可以发现,yield return 并不是等所有执行完了才一次性返回的。而是调用一次就返回一次结果的元素。这也就是按需供给。

解析定义类

我们已经大致了解了yield 的用法和它与平常的返回的区别。我们可以继续查看其运行原理。我们首先看这么一个案例(在0-10中随机返回五个数字):

我们通过SharpLab反编译其代码,我们进行查看发现yield具体详细实现:

 

我们看到yield内部含有一个迭代器。这样去实现的迭代遍历。同时包含_state字段、用来存储上一次的记录。_current包含当前的值、也通过_initialThreadId获取当前线程id。其中主要的方法是迭代器方法MoveNext()。我们根据反编译结果来实现一个与yiled相似的类:

    /// <summary>
/// 解析yield并定义相似类
/// </summary>
public sealed class GetRandomNumbersClass : IEnumerable<int>, IEnumerable, IEnumerator<int>, IDisposable, IEnumerator
{
public static Random r = new Random(); /// <summary>
/// 状态
/// </summary>
private int _state; /// <summary>
///储存当前值
/// </summary>
private int _current; /// <summary>
/// 线程id
/// </summary>
private int _initialThreadId; /// <summary>
/// 集合元素数量
/// </summary>
private int count; /// <summary>
/// 集合元素数量
/// </summary>
public int _count; /// <summary>
/// 当前指针
/// </summary>
private int i; int IEnumerator<int>.Current
{
[DebuggerHidden]
get
{
return _current;
}
} object IEnumerator.Current
{
[DebuggerHidden]
get
{
return _current;
}
} [DebuggerHidden]
public GetRandomNumbersClass(int state)
{
this._state = state;
_initialThreadId = Environment.CurrentManagedThreadId;
} [DebuggerHidden]
void IDisposable.Dispose()
{
} private bool MoveNext()
{
switch (_state)
{
default:
return false;
case :
_state = -;
i = ;
break;
case :
_state = -;
i++;
break;
}
if (i < count)
{
_current = r.Next();
_state = ;
return true;
}
return false;
} bool IEnumerator.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
return this.MoveNext();
} [DebuggerHidden]
void IEnumerator.Reset()
{
throw new NotSupportedException();
} [DebuggerHidden]
public IEnumerator<int> GetEnumerator()
{
GetRandomNumbersClass _getRandom;
if (_state == - && _initialThreadId == Environment.CurrentManagedThreadId)
{
_state = ;
_getRandom = this;
}
else
{
_getRandom = new GetRandomNumbersClass();
}
_getRandom.count = _count;
return _getRandom;
} [DebuggerHidden]
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
} [IteratorStateMachine(typeof(GetRandomNumbersClass))]
private static IEnumerable<int> GetList(int count)
{
GetRandomNumbersClass getRandomNumbersClass = new GetRandomNumbersClass(-);
getRandomNumbersClass._count = count;
return getRandomNumbersClass;
}
private static void Main(string[] args)
{
IEnumerator<int> enumerator = GetList().GetEnumerator();
try
{
foreach (int item in GetList())
Console.WriteLine(item);
//while (enumerator.MoveNext())
//{
// int current = enumerator.Current;
// Console.WriteLine(current);
//}
}
finally
{
if (enumerator != null)
{
enumerator.Dispose();
}
}
Console.ReadKey();
}
}

 


 用爱生活,你会使自己幸福!用爱工作,你会使很多人幸福! 

   C#基础知识详解系列

   欢迎大家扫描下方二维码,和我一起学习更多的知识

C# 中yield关键字解析的更多相关文章

  1. C#中yield关键字理解

    yield关键字之前用得较少,但是在做项目开发的过程中也遇到了,当时有点迷惑,就顺便研究学习了一下,以下是个人理解,不到之处欢迎拍砖!废话就到这,上代码: class Program { static ...

  2. 理解 ES6 语法中 yield 关键字的返回值

    在 ES6 中新增了生成器函数的语法,本文解释了生成器函数内 yield 关键字的返回值. 描述 根据语法规范,yield 关键字用来暂停和继续执行一个生成器函数.当外部调用生成器的 next() 方 ...

  3. 理解 ES6 语法中 yield* 关键字的作用

    在 ES6 中新增了生成器函数的语法,本文解释了与生成器函数有关的 yield* 关键字,及其使用场景. 描述 根据语法规范,yield* 的作用是代理 yield 表达式,将需要函数本身产生(yie ...

  4. C/C++中extern关键字解析

    1 基本解释:extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义.此外extern也可用来进行链接指定. 也就是说extern ...

  5. Java中volatile关键字解析

    一.内存模型的相关概念 大家都知道,计算机在执行程序时,每条指令都是在CPU中执行的,而执行指令过程中,势必涉及到数据的读取和写入.由于程序运行过程中的临时数据是存放在主存(物理内存)当中的,这时就存 ...

  6. java中static关键字解析

    static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...

  7. java中this关键字解析

    由于this关键字在Java程序中经常见到,笔者索性把它的用法总结一下,来和大家一到互相学习一下.总的来说this用在下面几个地方: (1)当局部变量和成员变量同名的时候,需要用this来加以区分 如 ...

  8. .NET中的yield关键字

    浅谈yield http://www.cnblogs.com/qlb5626267/archive/2009/05/08/1452517.html .NET中yield关键字的用法 http://bl ...

  9. [转]Python中yield的解释

    转自: http://python.jobbole.com/83610/ 本文作者: 伯乐在线 - wklken .未经作者许可,禁止转载!欢迎加入伯乐在线 专栏作者. 翻译 来源于stackover ...

随机推荐

  1. Linux初识之VMWare14中配置Centos7桥接网络环境

    1.查看当前初始环境如下:

  2. PHP5.6版本在Windows上安装redis扩展

    PHP使用redis扩展 一.php安装redis扩展   1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本       2.根据PHP版本号,编译器版本号和CPU架构, 选择 ...

  3. C# 反射Reflection——反射反射程序员的快乐

    一.什么是反射 反射Reflection:System.Reflection,是.Net Framework提供的一个帮助类库,可以读取并使用metadata. 反射是无处不在的,MVC-Asp.Ne ...

  4. 死磕 java同步系列之Phaser源码解析

    问题 (1)Phaser是什么? (2)Phaser具有哪些特性? (3)Phaser相对于CyclicBarrier和CountDownLatch的优势? 简介 Phaser,翻译为阶段,它适用于这 ...

  5. java时间格式转换任意格式

    例如:20180918/120023转换成2018-09-18 12:00:23 //时间格式转换 public String getNomalTime(String oldTime){ String ...

  6. 虚拟现实中的Motion Sickness晕动症问题 - VIMS

    虚拟现实(VR)中的晕动症 - VIMS 在玩VR的时候,很多玩家都遇到过发晕恶心等症状,这就是晕动症(Motion Sickness,以下或简称MS).MS并不是VR特有的问题.我们在坐船.坐车.坐 ...

  7. CentOS系统查看软件安装路径

    Linux系统一般都是命令行界面,对于安装的软件也是通过命令安装的.对于软件包更新和卸载等有时候需要查看检查是否有改软件,软件安装存储的路径对于修改配置文件等是必要的.那么怎么查看软件安装路径呢?小编 ...

  8. VS Code配置Go语言开发环境(建议使用goland)

    VS Code是微软开源的一款编辑器,插件系统十分的丰富.本文就介绍了如何使用VS Code搭建Go语言开发环境. VS Code配置Go语言开发环境 说在前面的话,Go语言是采用UTF8编码的,理论 ...

  9. Spring 中使用 @Scheduled 创建定时任务

    一.定时任务触发条件 1.在 Application 启动类上添加:@EnableScheduling 2.含定时方法的类上添加注解:@Component,该注解将定时任务类纳入 spring bea ...

  10. linux分析工具之vmstat详解

    一.概述 vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.首先我们查看下帮助.如下图所 ...