yield return in C#
Yield has two great uses
It helps to provide custom iteration with out creating temp collections.
It helps to do stateful iteration
Iteration. It creates a state machine "under the covers" that remembers where you were on each additional cycle of the function and picks up from there.
e.g.
private static IEnumerable<string> GetIdList(DateTime startTime, DateTime endTime)
{
var collectionList = new List<string>(); for (var dateTime = new DateTime(startTime.Year, startTime.Month, 1); dateTime <= endTime; dateTime = dateTime.AddMonths(1))
{
collectionList.Add(dateTime.ToString("d"));
} return collectionList;
}
could be writen as:
private static IEnumerable<string> GetIdList(DateTime startTime, DateTime endTime)
{
for (var dateTime = new DateTime(startTime.Year, startTime.Month, 1); dateTime <= endTime; dateTime = dateTime.AddMonths(1))
{
yield return collectionList.Add(dateTime.ToString("d"));
}
}
- It helps to provide custom iteration with out creating temp collections.
It helps to do stateful iteration.
- In order to explain the above two points more demonstratively, I have created a simple video and the link for same is here: http://www.youtube.com/watch?v=4fju3xcm21M
yield return in C#的更多相关文章
- 可惜Java中没有yield return
项目中一个消息推送需求,推送的用户数几百万,用户清单很简单就是一个txt文件,是由hadoop计算出来的.格式大概如下: uid caller 123456 12345678901 789101 12 ...
- C#中的using和yield return混合使用
最近写代码为了为了省事儿用了几个yield return,因为我不想New一个List<T>或者T[]对象再往里放元素,就直接返回IEnumerable<T>了.我的代码里还有 ...
- yield return的用法简介
使用yield return 语句可一次返回一个元素. 迭代器的声明必须满足以下要求: 返回类型必须为 IEnumerable.IEnumerable<T>.IEnumerator 或 I ...
- yield return的作用
测试1: using UnityEngine; using System.Collections; public class test1 : MonoBehaviour { // Use this f ...
- yield学习续:yield return迭代块在Unity3D中的应用——协程
必读好文推荐: Unity协程(Coroutine)原理深入剖析 Unity协程(Coroutine)原理深入剖析再续 上面的文章说得太透彻,所以这里就记一下自己的学习笔记了. 首先要说明的是,协程并 ...
- C#中yield return用法分析
这篇文章主要介绍了C#中yield return用法,对比使用yield return与不使用yield return的流程,更直观的分析了yield return的用法,需要的朋友可以参考下. 本文 ...
- C#中的yield return与Unity中的Coroutine(协程)(下)
Unity中的Coroutine(协程) 估计熟悉Unity的人看过或者用过StartCoroutine() 假设我们在场景中有一个UGUI组件, Image: 将以下代码绑定到Image using ...
- C#中的yield return与Unity中的Coroutine(协程)(上)
C#中的yield return C#语法中有个特别的关键字yield, 它是干什么用的呢? 来看看专业的解释: yield 是在迭代器块中用于向枚举数对象提供值或发出迭代结束信号.它的形式为下列之一 ...
- 12.C#yield return和yield break及实际应用小例(六章6.2-6.4)
晚上好,各位.今天结合书中所讲和MSDN所查,聊下yield关键字,它是我们简化迭代器的关键. 如果你在语句中使用了yield关键字,则意味着它在其中出现的方法.运算符或get访问器是迭代器,通过使用 ...
- yield return 和 yield break
//yield return 返回类型必须为 IEnumerable.IEnumerable<T>.IEnumerator 或 IEnumerator<T>. static I ...
随机推荐
- LNMP : 502 Bad Gateway 解决小记,真正的原因
站点搬迁到新的server.原先一直都是LAMP.如今改为LNMP. 将重写文件 htaccess改成 nginx的 conf.放到了站点.可仅仅能打开首页,其它重写页面一打开都是不停的载入. 载入等 ...
- ZOJ 3829 Known Notation (2014牡丹江H称号)
主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...
- 什么是CTS、CLS和CLR
CTS.CLS和CLR是.NET框架的基本概念,但往往成为程序员们忽视的部分.清楚理解三者的概念,有助于.NET程序员编写高效安全的.NET程序. 所涉及到的知识点 • .NET的基本概念 • ...
- spring mvc 返回json数据的四种方式
一.返回ModelAndView,其中包含map集 /* * 返回ModelAndView类型的结果 * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式 ...
- Eclipse+EGit的配置注意点, 以及解决Github多个本地仓库之间的冲突
问题描述 不同本地仓库(e.g. Repo1, Repo2)之间同时修改一个文件时, 出现文件无法merge的情况. 具体表现为, 冲突(红色双向实心箭头)一直存在, 点pull没反应, 点push报 ...
- IM,游戏服务端 tcp 框架整理
Mina: Mina(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应 ...
- PROS Step:只需几分钟即可创建优化的价目表,并发现即时收益机会。
多年来,各个公司一直使用手动流程和电子表格来制定产品和服务定价,而没有真正意义上的方法或策略.在我写这篇文章时仍然如此! 但是,如今的形势已经改变.利用 PROS Step,公司可以将其数据上传到 M ...
- [置顶] C#扩展方法 扩你所需
通过前面的学习,了解到:使用扩展方法,可以向现有类型“添加”方法.本文将使用扩展方法来对系统类型,自定义类型及接口进行方法扩展,一睹扩展方法的风采. 1.使用扩展方法来扩展系统类型 String是c# ...
- MyEclipse中jsp的凝视报错解决
jsp页面中凝视报错: 出错现场:在eclipse中没有报错.在MyEclipse中报错. <!-- To use express install, set to playerProductIn ...
- c++ 覆盖、重载、隐藏
函数重载: 1.相同的范围内(即同一类中) 2.函数名相同: 3.参数不同: 4.virtual关键字可有可无: 函数覆盖:虚函数的功能.动态多态 (父类中必须有virtual)========派生类 ...