使用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. c#静态构造函数

    作用是初始化一些类的静态成员 1.在实例化类的对象,或者引用任何的静态成员之前,.Net自动调用此构造函数,而且只调用一次 2.没有任何修饰符,也没有参数 3.一个类只能有一个静态构造函数 4.无参的 ...

  2. STL UVA 11991 Easy Problem from Rujia Liu?

    题目传送门 题意:训练指南P187 分析:用vector存id下标,可以用map,也可以离散化用数组存(发现不用离散化也可以) map #include <bits/stdc++.h> u ...

  3. DHL 快递跟踪查询

      思路描述:主要使用正则表达式解析. 返回一个跟踪步骤列表. public class TrackingData     {         public string time { get; se ...

  4. eclipse svn proxy

    C:\Users\userName\AppData\Roaming\Subversion下的servers文件, 将#http-proxy-host和#http-proxy-port这两行前面的#号去 ...

  5. HDU 2838 (DP+树状数组维护带权排序)

    Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...

  6. Leetcode Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  7. ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]的用法

    父类:public class BaseHibernateDaoSupport<T>{ private Class<T> entityClass; public BaseHib ...

  8. Bootstrap_Datatable Ajax请求两次问题的解决

    最近一个项目中使用JQuery Datatable,用起来比较方便,但在测试过程中,发现当条件改变时,有时查询结果中的数据不正确. 使用FireBug跟踪时,发现在使用Ajax请求时,点击一次搜索按钮 ...

  9. 20145330孙文馨 《Java程序设计》第一周学习总结

    20145330孙文馨 <Java程序设计>第一周学习总结 教材学习内容总结 刚开始拿到这么厚一本书说没有压力是不可能的,开始从头看觉得很陌生进入不了状态,就稍微会有一点焦虑的感觉.于是就 ...

  10. 最新Velocity使用和Velocity语法

    Velocity语法 Velocity的使用要用到下面几个包,可以从官网下载,commons-collections.jar,velocity-1.4.jar,velocity-dept.jar; 1 ...