yield迭代器的使用
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
for (int i = 0; i < 10; i++)
{
students.Add(new Student() { Age = i, Name = "名字" + i });
} {
Console.WriteLine("********************未使用迭代器 Start*****************************");
List<Student> query = students.WhereNoYield(s =>
{
Thread.Sleep(300);
return s.Age > 0;
});
foreach (var item in query)
{
Console.WriteLine($"{DateTime.Now} - {item.Age} - {item.Name}");
}
Console.WriteLine("********************未使用迭代器 End*****************************");
} {
Console.WriteLine("********************使用迭代器 Start*****************************");
IEnumerable<Student> query = students.WhereWithYield(s =>
{
Thread.Sleep(300);
return s.Age > 0;
});
foreach (var item in query)
{
Console.WriteLine($"{DateTime.Now} - {item.Age} - {item.Name}");
}
Console.WriteLine("********************使用迭代器 End*****************************");
} Console.ReadKey();
}
} public static class MyClass
{
public static IEnumerable<T> WhereWithYield<T>(this IEnumerable<T> list, Func<T, bool> func)
{
foreach (var item in list)
{
if (func.Invoke(item))
{
yield return item;
}
}
} public static List<T> WhereNoYield<T>(this List<T> list, Func<T, bool> func)
{
List<T> lists = new List<T>();
foreach (var item in list)
{
if (func.Invoke(item))
{
lists.Add(item);
}
}
return lists;
}
} public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
通过运行结果时间可以看出:
未使用迭代器,等待“WhereNoYield”函数运算完成后,再进行打印数据
使用迭代器,每次执行“WhereWithYield“函数时,会直接打印数据

yield迭代器的使用的更多相关文章
- 学习笔记: yield迭代器
yield 与 IEnumerable<T> 结对出现, 可实现按需获取 , 迭代器模式 static void Main(string[] args) { ...
- node 异步回调解决方法之yield
先看如何使用 使用的npm包为genny,npm 安装genny,使用 node -harmony 文件(-harmony 为使用es6属性启动参数) 启动项目 var genny= require( ...
- py3.0第四天 函数,生成器迭代器等
1.列表生成式,迭代器&生成器 孩子,我现在有个需求,看列表[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],我要求你把列表里的每个值加1,你怎么实现?你可能会想到2种方式 > ...
- Python函数——列表推导式、生成器与迭代器
列表推导式 产生背景 现在有个需求,看列表[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],要求你把列表里的每个值加1,你怎么实现? 第一种方法: a = [1,3,4,6,7,7,8,9 ...
- 生成器&迭代器,模块
列表生成式 将列表data=[1,2,3]里的元素都乘2 方法一 data=[1,2,3] for index,i in enumerate(data): data[index] *=2 print( ...
- Python学习之路7 - 生成器&迭代器
本章内容: 列表生成式 生成器 yield 迭代器 列表生成式 当我们要定义一个列表的时候,我们通常用这种方式a = [1,2,3],但是如果我们定义了一个比较长的列表的时候,手动定义列表就会比较麻烦 ...
- yield关键字的使用
yield的中文是什么意思呢? 在金山词霸上面的翻译是: vt.屈服,投降: 生产: 获利: 不再反对 vi.放弃,屈服: 生利: 退让,退位 n.产量,产额: 投资的收益: 屈服,击穿: 产品 个人 ...
- 一文说通C#中的异步迭代器
今天来写写C#中的异步迭代器 - 机制.概念和一些好用的特性 迭代器的概念 迭代器的概念在C#中出现的比较早,很多人可能已经比较熟悉了. 通常迭代器会用在一些特定的场景中. 举个例子:有一个for ...
- ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await
ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await co ...
随机推荐
- python正常时间和unix时间戳相互转换的方法
python正常时间和unix时间戳相互转换的方法 本文实例讲述了python正常时间和unix时间戳相互转换的方法.分享给大家供大家参考.具体分析如下: 这段代码可以用来转换常规时间格式为unix时 ...
- ubuntu安装完成后需要做的事情
1.删除libreoffice libreoffice虽然是开源的,但是Java写出来的office执行效率实在不敢恭维,装完系统后果断删掉 [html] view plain copy sudo a ...
- vbox出现Failed to opencreate the internal network错误,无法启动虚拟机
vbox出现Failed to opencreate the internal network错误,无法启动虚拟机 标签(空格分隔): 未分类 问题 Failed to open/create the ...
- centos7.3 安装gitlab
系统自带ruby版本太低,需要手动编译2.4版本
- 高级UI晋升之自定义View实战(六)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从Android 自定义属性动画&Camera动画来介绍自定义V ...
- springboot多数据库及分布式事务配置
1.导入相应的jar包依赖 <!-- 集成mybatis --> <dependency> <groupId>org.mybatis.spring.boot< ...
- Codesforces 485D Maximum Value
D. Maximum Value You are given a sequence a cons ...
- window 下总是object_detection/protos/*.proto: No such file or directory
这是因为目前的protoc3.5有Bug,换成3.4就好了https://github.com/google/protobuf/releases/tag/v3.4.0
- 请问如何实现字符串UTF8->BIG5,BIG5->UTF8。保证送分。-Java/JavaSE
请问如何实现字符串UTF8-> BIG5,BIG5-> UTF8. ------回答--------- ------其他回答(100分)--------- public String BI ...
- ICPC2008哈尔滨-A-Array Without Local Maximums
题目描述 Ivan unexpectedly saw a present from one of his previous birthdays. It is array of n numbers fr ...