使用yield return 语句可一次返回一个元素。

迭代器的声明必须满足以下要求:

 返回 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的用法简介的更多相关文章

  1. C#中yield return用法分析

    这篇文章主要介绍了C#中yield return用法,对比使用yield return与不使用yield return的流程,更直观的分析了yield return的用法,需要的朋友可以参考下. 本文 ...

  2. C# yield return 用法与解析

    原文:C# yield return 用法与解析 C# yield return 用法与解析 本文参考自:http://www.jb51.net/article/54810.htm 当初没有认真理解 ...

  3. C# yield return用法

    本文实例讲述了C#中yield return用法,并且对比了使用yield return与不使用yield return的情况,以便读者更好的进行理解.具体如下: yield关键字用于遍历循环中,yi ...

  4. sleep、yield、join方法简介与用法 sleep与wait区别 多线程中篇(十五)

    Object中的wait.notify.notifyAll,可以用于线程间的通信,核心原理为借助于监视器的入口集与等待集逻辑 通过这三个方法完成线程在指定锁(监视器)上的等待与唤醒,这三个方法是以锁( ...

  5. C#中的yield return用法演示源码

    下边代码段是关于C#中的yield return用法演示的代码. using System;using System.Collections;using System.Collections.Gene ...

  6. C#中yield return用法

    转载:http://www.jb51.net/article/54810.htm http://www.cnblogs.com/HunterWei/archive/2012/06/13/csharpy ...

  7. IOS NSInvocation用法简介

    IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...

  8. JodaTime用法简介

    JodaTime用法简介 Java的Date和Calendar用起来简直就是灾难,跟C#的DateTime差距太明显了,幸好有JodaTime 本文简单罗列JodaTime的用法 package co ...

  9. yield关键字的用法

    在上一篇文章中,说了下foreach的用法,但是还是比较复杂的,要实现接口才能进行遍历,有没有简单些的方法呢?答案是肯定的.且看下面. yield关键字的用法: 1.为当前类型添加一个任意方法,但是要 ...

随机推荐

  1. NUC_HomeWork1 -- POJ2067(最短路)

    C - Fire Station Description A city is served by a number of fire stations. Some residents have comp ...

  2. linux 安装程序

    tar文件打开 tar -xvf myfile.tar bz2文件打开

  3. ACM 一种排序

    一种排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数 ...

  4. jQuery 循环问题

    $("#add2sub").click(function(){ var $sxarr=$(".add_shuxing_ul > .sx_add_bg"); ...

  5. Linux open函数

    Linux open函数 open 函数用于打开和创建文件.以下是 open 函数的简单描述 #include <fcntl.h> int open(const char *pathnam ...

  6. Equal Sum Sets

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49406 题意: 输入n,k,s,求在不小于n的数中找出k个不同的数 ...

  7. 对AFN的二次封装

     .h .m

  8. HashMap封装的数据用循环快速添加进list中产生的数据集全部相同的问题

    一.问题概述 在一次使用SimpleAdapter时,Data需要使用传入一条数据(Image.Text),该数据条使用HashMap封装.在用HashMap封装的数据用循环快速添加进list中产生了 ...

  9. iptables防火墙作为基本需求的配置

    企业中使用iptables防火墙:(一般不要在命令中输入规则) # Firewall configuration written by system-config-firewall# Manual c ...

  10. Jfinal中手动提交/回滚 事物

    在Jfinal中有个Tx类为事物声明类 在方法或controller上面加@Before({Tx.class})即可,可是这样并不能满足有的业务场景 下面是今天写的手动提交的事物处理方法,希望对大家有 ...