使用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. http://www.blogjava.net/zhangchao/archive/2011/05/26/351051.html

    http://www.blogjava.net/zhangchao/archive/2011/05/26/351051.html

  2. jQuery操作列表数据转成Json再输出为html dom树

    jQuery 把列表数据转成Json再输出为如下 dom树 <div id="menu" class="lv1"> <ul class=&qu ...

  3. iOS开发实用干货——强化你的Xcode控制台

    f(x) 郑秀晶程序员不要整天看代码,偶尔也要看看风景?? www.90168.org先上一张我的Xcode控制台的图片让你们感受一下 酷炫控制台 是不是觉得很酷?不过仅仅是酷还是远远不够的,当你点击 ...

  4. 每天一个Linux命令---tcpdump

    用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的“头” ...

  5. uva 107 - The Cat in the Hat

     The Cat in the Hat  Background (An homage to Theodore Seuss Geisel) The Cat in the Hat is a nasty c ...

  6. poj2524-Ubiquitous Religions

    C - Ubiquitous Religions Time Limit: 5000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d ...

  7. [Word]将word文件中的软回车符[↓]替换为硬回车符

    Ctrl+H,替换对话框 搜索:^l 替换:^p 确定替换即可.

  8. BZOJ2934 : [Poi1999]祭坛问题

    对于每个祭坛,算出每条线段阻碍它的角度区间,然后排序求并看看是否有空位即可,时间复杂度$O(n^2\log n)$. 这题在Main上官方时限是0.2S,因此需要几个常数优化: $1.$为了避免用at ...

  9. 关于NSNotificationCenter消息通信用法

    NSNotificationCenter主要用于广播消息到多个监听着,其传统用法 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotification ...

  10. jquery实现隐藏,简化和更多

    HTML代码: <div class="box"> <div class="header"> <h3>图书分类</h3 ...