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.为当前类型添加一个任意方法,但是要 ...
随机推荐
- Linux(CentOS)中安装MongoDB
1. 下载对应操作系统和机器的版本 网址: https://www.mongodb.com/download-center?jmp=docs 我的系统为CentOS-6.6 ,64位,所以选择红帽子版 ...
- Codeforces Round #344 (Div. 2)
水 A - Interview 注意是或不是异或 #include <bits/stdc++.h> int a[1005], b[1005]; int main() { int n; sc ...
- HDU5754 Life Winner Bo(博弈)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5754 Description Bo is a "Life Winner" ...
- Kalman滤波器原理和实现
Kalman滤波器原理和实现 kalman filter Kalman滤波器的直观理解[1] 假设我们要测量一个房间下一刻钟的温度.据经验判断,房间内的温度不可能短时大幅度变化,也就是说可以依经验认为 ...
- mybatis 查询缓存问题
<settings> <setting name="localCacheScope" value="STATEMENT" /> < ...
- 通过Spring Mail Api发送邮件
使用Java Mail API来发送邮件也很容易实现,但是最近公司一个同事封装的邮件API实在让我无法接受,于是便打算改用Spring Mail API来发送邮件,顺便记录下这篇文章. [Spring ...
- ACM 喷水装置(二)
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...
- 【HDU】4336 Card Collector
http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意:n张卡片,每一次取一个盒子,盒子里装有卡片i的概率是p[i],求得到所有卡片所需要开的盒子的期望数( ...
- UESTC 1307 windy数(数位DP)
题目链接 这其实入门题,不过,我写了好一会,还是一直wa,图样... #include <iostream> #include <cstdio> #include <cs ...
- 崩溃恢复(crash recovery)与 AUTORESTART参数
关于这个参数设置的影响,在生产系统中经历过两次: 第一次是有套不太重要的系统安装在虚拟机,这套系统所有应用(DB2 WAS IHS)都配置到/etc/rc.local中,每次启动机器会自 ...