C#中YieldReturn的用法
Yield Return 和 Yield Break 的出现是为了简化迭代器。
类如果能被遍历则必须有IEnumerator<string> GetEnumerator() 方法, 并有用Yield Return
方法如果能被遍历则返回类型为 IEnumerable<string>,并有用Yield Return
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace TestYield
{
class Program
{
static void Main(string[] args)
{ foreach (string v in new Animal())
{
Console.WriteLine(v);
} foreach (string v in getList())
{
Console.WriteLine(v);
}
Console.ReadLine();
} static IEnumerable<string> getList()
{
yield return "cccc";
yield return "dddd";
} } public class Animal {
//类迭代必须要有的方法,使用Yield简化了迭代的实现
public IEnumerator<string> GetEnumerator()
{
yield return "aaaaa";
yield return "bbbbb";
} }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestYield
{
class Program
{
static void Main(string[] args)
{
foreach (string v in new Animal())
{
Console.WriteLine(v);
}
foreach (string v in getList())
{
Console.WriteLine(v);
}
Console.ReadLine();
}
static IEnumerable<string> getList()
{
yield return "cccc";
yield return "dddd";
}
}
public class Animal {
public IEnumerator<string> GetEnumerator()
{
yield return "aaaaa";
yield return "bbbbb";
}
}
}
C#中YieldReturn的用法的更多相关文章
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- SQL中distinct的用法
SQL中distinct的用法 1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...
- Oracle 中 decode 函数用法
Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...
- jQuery中Animate进阶用法(一)
jQuery中animate的用法你了解多少呢?如果仅仅是简单的移动位置,显示隐藏,哦!天哪你在浪费资源!因为animate太强大了,你可以有很多意想不到的用法!让我们一起研究一下吧~~ 首先要了解j ...
- [转载]js中return的用法
一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return; 在大多数情况下,为事件处理函 ...
- js中this的用法
经过近几周的模拟面试题,我查询了一些资料,今天就来说说,在js中this的用法吧.方法有四:第一,用作全局变量,第二,用作表该对象,第三,用作构造函数,第四,用作call和applay
- jQuery中eq()方法用法实例
本文实例讲述了jQuery中eq()方法用法.分享给大家供大家参考.具体分析如下: 此方法能够获取匹配元素集上的相应位置索引的元素. 匹配元素集上元素的位置索引是从0开始的. 语法结构: 复制代码 代 ...
- php中return的用法实例分析
本文实例讲述了php中return的用法.分享给大家供大家参考.具体分析如下: 首先,它的意思就是返回;return()是语言结构而不是函数,仅在参数包含表达式时才需要用括号将其括起来.当返回一个变量 ...
随机推荐
- Python的并发并行[1] -> 线程[0] -> threading 模块
threading模块 / threading Module 1 常量 / Constants Pass 2 函数 / Function 2.1 setprofile()函数 函数调用: thread ...
- 求int最大值以及int二进制
求int最大值:(((unsigned int)(~0))>>1) 求int的2进制串 string str = ""; int iNum = 100; for(int ...
- 深入JS正则先行断言
这里是 Mastering Lookahead and Lookbehind 文章的简单翻译,这篇文章是在自己搜索问题的时候stackoverflow上回答问题的人推荐的,看完觉得写得很不错.这里的简 ...
- Delphi CRC算法, 不错
http://www.cnblogs.com/tangqs/archive/2011/12/08/2280255.html
- fuser 和 lsof
FUSER fuser功能fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息. fuser显示使用指定文件或者文件系统的进程ID.默认情况下每个文 ...
- 玩转shell之符号篇
转:http://hi.baidu.com/hellosimple/item/21b31dfefd23e811e2e3bd47 在shell中常用的特殊符号罗列如下: # ; ;; . , / \\ ...
- CGCS2000坐标系与其他坐标系间的差异和转换方法
转自 CGCS2000坐标系与其他坐标系间的差异和转换方法 1954北京坐标系和1980西安坐标系是以天文大地网等经典测量技术为基础的局部坐标系. CGCS2000是以地球质量中心为原点的地心大地坐 ...
- VirtualBox – Error In supR3HardenedWinReSpawn
Genymotion 模拟器安装好虚拟机后,启动时报错: ————————— VirtualBox – Error In supR3HardenedWinReSpawn ————————— <h ...
- 设计模式之迭代器模式(PHP实现)
github地址:https://github.com/ZQCard/design_pattern/** * 迭代器模式(Iterator Pattern)是 Java 和 .Net 编程环境中非常常 ...
- applicationContext.xml文件如何调用外部properties等配置文件
只需要在applicationContext.xml文件中添加一行: <!-- 导入外部的properties文件 --> <context:property-placeholder ...