yield return的用法简介
- 使用yield return 语句可一次返回一个元素。
迭代器的声明必须满足以下要求:
返回类型必须为 IEnumerable、IEnumerable<T>、IEnumerator 或 IEnumerator<T>。
返回 IEnumerable 或 IEnumerator 的迭代器的 yield 类型为 object。如果迭代器返回 IEnumerable<T> 或 IEnumerator<T>,则必须将yield return 语句中的表达式 类型隐式转换为泛型类型参数。
你不能在具有以下特点的方法中包含 yield return 或 yield break 语句:
匿名方法。
包含不安全的块的方法。
- public class Student
- {
- public String Name { get; set; }
- public int Age { get; set; }
- //代码更简洁
- public IEnumerable<Student> Students
- {
- get
- {
- yield return new Student { Name = "Tadpole", Age = 400 };
- yield return new Student { Name = "Pinwheel", Age = 25 };
- yield return new Student { Name = "Milky Way", Age = 0 };
- yield return new Student { Name = "Andromeda", Age = 3 };
- }
- }
- public IEnumerable<Student> Students2
- {
- get
- {
- List<Student> list = new List<Student>();
- list.Add(new Student { Name = "Tadpole", Age = 400 });
- list.Add(new Student { Name = "Tadpole", Age = 400 });
- list.Add(new Student { Name = "Tadpole", Age = 400 });
- list.Add(new Student { Name = "Tadpole", Age = 400 });
- list.Add(new Student { Name = "Tadpole", Age = 400 });
- return list;
- }
- }
- }
- //将不会返回空
- public static IEnumerable<string> Test()
- {
- yield break;
- }
yield return的用法简介的更多相关文章
- C#中yield return用法分析
这篇文章主要介绍了C#中yield return用法,对比使用yield return与不使用yield return的流程,更直观的分析了yield return的用法,需要的朋友可以参考下. 本文 ...
- C# yield return 用法与解析
原文:C# yield return 用法与解析 C# yield return 用法与解析 本文参考自:http://www.jb51.net/article/54810.htm 当初没有认真理解 ...
- C# yield return用法
本文实例讲述了C#中yield return用法,并且对比了使用yield return与不使用yield return的情况,以便读者更好的进行理解.具体如下: yield关键字用于遍历循环中,yi ...
- sleep、yield、join方法简介与用法 sleep与wait区别 多线程中篇(十五)
Object中的wait.notify.notifyAll,可以用于线程间的通信,核心原理为借助于监视器的入口集与等待集逻辑 通过这三个方法完成线程在指定锁(监视器)上的等待与唤醒,这三个方法是以锁( ...
- C#中的yield return用法演示源码
下边代码段是关于C#中的yield return用法演示的代码. using System;using System.Collections;using System.Collections.Gene ...
- C#中yield return用法
转载:http://www.jb51.net/article/54810.htm http://www.cnblogs.com/HunterWei/archive/2012/06/13/csharpy ...
- IOS NSInvocation用法简介
IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...
- JodaTime用法简介
JodaTime用法简介 Java的Date和Calendar用起来简直就是灾难,跟C#的DateTime差距太明显了,幸好有JodaTime 本文简单罗列JodaTime的用法 package co ...
- yield关键字的用法
在上一篇文章中,说了下foreach的用法,但是还是比较复杂的,要实现接口才能进行遍历,有没有简单些的方法呢?答案是肯定的.且看下面. yield关键字的用法: 1.为当前类型添加一个任意方法,但是要 ...
随机推荐
- http://blog.csdn.net/jyw935478490/article/details/51233931
http://blog.csdn.net/jyw935478490/article/details/51233931
- [转载] Spring MVC - 处理器拦截器
5.1.处理器拦截器简介 Spring Web MVC的处理器拦截器(如无特殊说明,下文所说的拦截器即处理器拦截器)类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理. ...
- UVa1486 Transportation(最小费用最大流)
题目大概说有n个城市,m条有向边连着它们,每条边都有两个属性一个是ai一个是ci,ai表示运送x单位货物时花费ai*x*x的价钱,ci表示边最多能运送的货物数量(<=5).问从城市1运送k单位货 ...
- dplyr包--数据操作与清洗
1.简介 在我们数据分析的实际应用中,我们可能会花费大量的时间在数据清洗上,而如果使用 R 里面自带的一些函数(base 包的 transform 等),可能会觉得力不从心,或者不是很人性化.好在我们 ...
- 80端口被占用,pid=4强制杀进程杀不掉
解决: http://www.cnblogs.com/myjavawork/articles/1867839.html 把SqlServer的该服务关闭.虽然他显示的进程号跟搜出来的不一样.
- CF#335 Lazy Student
Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #215 (Div. 2) C. Sereja and Algorithm
#include <iostream> #include <vector> #include <algorithm> #include <string> ...
- 20161003 NOIP 模拟赛 T2 解题报告
Weed duyege的电脑上面已经长草了,经过辨认上面有金坷垃的痕迹. 为了查出真相,duyege 准备修好电脑之后再进行一次金坷垃的模拟实验. 电脑上面有若干层金坷垃,每次只能在上面撒上一层高度为 ...
- Android -- TabHost
TabHost 也就相当于Windows下的选项框 有两种实现方式 1. 继承TabActivity (已经废弃):从TabActivity中用getTabHost()方法获取TabHost 2. ...
- C#_生成HTML
#region 生成静态页 /// <summary> /// 生成静态页 /// </summary> /// <param name="URL"& ...