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.为当前类型添加一个任意方法,但是要 ...
随机推荐
- iOS 安装Cocoapods以及安装第三方库的操作流程
安装cocoapods的流程: 1.打开终端,输入: sudo gem update —system 2.输入密码,稍等 3.gem sources --remove https://rubygem ...
- Code Rush插件
code rush 是微软推出的一款VS2008上的插件.他有强大的文件和代码导航功能,易于访问的重构和代码创建功能.一组编辑器.选择.剪贴板工具等. 教程链接 http://www.devexpre ...
- ural 2065. Different Sums
2065. Different Sums Time limit: 1.0 secondMemory limit: 64 MB Alex is a very serious mathematician ...
- Surf特征提取分析
Surf特征提取分析 Surf Hessian SIFT 读"H.Bay, T. Tuytelaars, L. V. Gool, SURF:Speed Up Robust Features[ ...
- 【原】iOS学习之XML与JSON两种数据结构比较和各自底层实现
1.XML与JSON两种数据结构的优缺点 1> XML 优点: 格式统一, 符合标准 容易与其他系统进行远程交互, 数据共享比较方便 缺点: XML文件格式文件庞大, 格式复杂, 传输占 ...
- ccc 音乐播放
cc.Class({ extends: cc.Component, properties: { musicPlayer: { default: null, type: cc.AudioSource } ...
- 转载:CSS3 Flexbox可视化指南
0. 目录 目录 引言 正文 1 引入 2 基础 3 使用 4 弹性容器Flex container属性 41 flex-direction 42 flex-wrap 43 flex-flow 44 ...
- ACM:统计难题 解题报告-字典树(Trie树)
统计难题 Time Limit:2000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Submit Status ...
- C语言(2)
C语言(2)---变量 基本格式: 变量类型 变量名1[,变量名2,变量名3,...变量名n]: 注意: 1.在C语言中如果申请一个变量,里面存放小数,则用float表示,且在输出时需要注意prin ...
- U-Boot编译过程解析
解压u-boot-2010.03.tar.bz2就可以得到全部U-Boot源程序.在顶层目录下有29个子目录,分别存放和管理不同的源程序.这些目录中所要存放的文件有其规则,可以分为3类. ● 与处理器 ...