前言

  前段时间了解到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. 前台提交数据到node服务器(get方式)

    .有两种办法,一种是表单提交,一种是ajax方式提交. 1.form提交  在前台模板文件上写: <form action="/reg" method="get&q ...

  2. 回顾TCP的三次握手过程

    在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接.第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认: SYN:同 ...

  3. ETL-Kettle学习笔记(入门,简介,简单操作)

    KETTLE Kettle:简介 ETL:简介 ETL(Extract-Transform-Load的缩写,即数据抽取.转换.装载的过程),对于企业或行业应用来说,我们经常会遇到各种数据的处理,转换, ...

  4. 数据分析--pandas的基本使用

    一.pandas概述 1.pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的. 2.pandas的主要功能 具备对其功能的数据结构DataFrame.Series 集成时间序 ...

  5. Android adb shell am 命令学习(1)

    am:activity manager 启动Activity,打开或关闭进程,发送广播等操作 为什么学习: 主要应用部分,后台启动对应的package的Activity adb shell am st ...

  6. Spring MVC-从零开始-@ResponseBody(未完待续)

    Spring MVC-从零开始-@ResponseBody(未完待续)

  7. Python爬虫:获取JS动态内容

    经过一段时间的python学习,能写出一些爬虫了.但是,遇到js动态加载的网页就犯了难.于是乎谷歌.百度,发现个好介绍http://www.jianshu.com/p/4fe8bb1ea984 主要就 ...

  8. Java文件操作——File

    创建File isFile().length().exists().createNewFile(). File.separator / isDirtory(). mkdir().mkdirs(). d ...

  9. [Note] GNUstep on Windows

    1.下载与安装 www.gnustep.org/windows/installer.html 下载 GNUstep MSYS System GNUstep Core GNUstep Devel 并安装 ...

  10. 快学Scala 第五课 (构造映射,获取映射值,更新映射值,迭代映射,与Java互操作)

    构造映射: val score = Map[String, Int]() val score1 = HashMap[String, Int]() val value1 = Map[String, In ...