yield return,yield break

转自, http://www.cnblogs.com/kingcat/archive/2012/07/11/2585943.html

yield return 表示在迭代中下一个迭代时返回的数据,除此之外还有yield break, 其表示跳出迭代,为了理解二者的区别我们看下面的例子

class A : IEnumerable
{
    private int[] array = new int[10];

public IEnumerator GetEnumerator()
    {
        for (int i = 0; i < 10; i++)
        {
            yield return array[i];
        }
    }
}

yield(C# 参考)  在迭代器块中用于向枚举数对象提供值或发出迭代结束信号。它的形式为下列之一:  yield return <expression>;  yield break;

   备注 :  计算表达式并以枚举数对象值的形式返回;expression 必须可以隐式转换为迭代器的 yield 类型。  yield 语句只能出现在 iterator 块中,该块可用作方法、运算符或访问器的体。这类方法、运算符或访问器的体受以下约束的控制:  不允许不安全块。  方法、运算符或访问器的参数不能是 ref 或 out。  yield 语句不能出现在匿名方法中。  当和 expression 一起使用时,yield return 语句不能出现在 catch 块中或含有一个或多个 catch 子句的 try 块中。  yield return 提供了迭代器一个比较重要的功能,即取到一个数据后马上返回该数据,不需要全部数据装入数列完毕,这样有效提高了遍历效率。

When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. Using yieldto define an iterator removes the need for an explicit extra class (the class that holds the state for an enumeration, see IEnumerator<T> for an example) when you implement the IEnumerable and IEnumerator pattern for a custom collection type.

如果你只想让用户访问ARRAY的前8个数据,则可做如下修改.这时将会用到yield break,修改函数如下

public IEnumerator GetEnumerator()
{
    for (int i = 0; i < 10; i++)
    {
        if (i < 8)
        {
            yield return array[i];
        }
        else
        {
            yield break;
        }
    }
}

时间: 2024-07-30 06:24:24

yield return,yield break的相关文章

C# yield return; yield break;

using System; using System.Collections; namespace YieldDemo { class Program { public static IEnumerable Power(int num, int exponent) { int counter = 0; int result = 1; while (counter++ < exponent) { //if (counter == 4) yield break; if (counter == 4)

C#中的yield return与Unity中的Coroutine(协程)

C#中的yield return C#语法中有个特别的关键字yield, 它是干什么用的呢? 来看看专业的解释: yield 是在迭代器块中用于向枚举数对象提供值或发出迭代结束信号.它的形式为下列之一:yield return <expression>;yield break 看如下例子: 1 public class CustomCollection :IEnumerable { 2 3 public static void Main (string[] args) 4 { 5 Custom

yield return 和 yield break

//yield return 返回类型必须为 IEnumerable.IEnumerable<T>.IEnumerator 或 IEnumerator<T>. static IEnumerator<int> yieldTest() //yield return 返回IEnumerator  { yield return 1; yield return 4; if (true)//如果为True 输出 1,4;//如果是False 输出 1,4,3,2 { yield b

12.C#yield return和yield break及实际应用小例(六章6.2-6.4)

晚上好,各位.今天结合书中所讲和MSDN所查,聊下yield关键字,它是我们简化迭代器的关键. 如果你在语句中使用了yield关键字,则意味着它在其中出现的方法.运算符或get访问器是迭代器,通过使用yield定义迭代器,可在实现自定义集合类型的IEnumerable和IEnumerator模式时无需显示类(保留枚举状态类),使用yield有两种形式,如下 1 yield return 表达式 2 yield break 先说明一下yield return语句,每一次返回一个元素.通过forea

C# yield return用法

本文实例讲述了C#中yield return用法,并且对比了使用yield return与不使用yield return的情况,以便读者更好的进行理解.具体如下: yield关键字用于遍历循环中,yield return用于返回IEnumerable<T>,yield break用于终止循环遍历. 有这样的一个int类型的集合: ? 1 2 3 4 static List<int> GetInitialData() {   return new List<int>(){

造轮子_C#中yield return用法分析

厂址: http://www.jb51.net/article/54810.htm static List<int> GetInitialData() {   return new List<int>(){1,2,3,4}; } 打印出所有值大于2的元素 不使用yield return的实现 static IEnumerable<int> FilterWithoutYield() {   List<int> result = new List<int&

C#基础复习IEnumerable(和 yield return的使用滴呀 )

IEnumerable 真是基础中的基础,然而..... 我们直接来看看这个接口的实现吧: 它是一个公开枚举数,该枚举数支持在非泛型集合上进行简单的迭代.换句话说,对于所有数组的遍历,都来自IEnumerable,那么我们就可以利用这个特性,来定义一个能够遍历xxxxxx的通用方法 先看我们的经典实例1: using System; using System.Collections; using System.Collections.Generic; using System.Linq; usi

yield return简单示例

List<int> list = new List<int>() { 1,2,3,4 }; // 需求:返回所有大于n的数 // 方法1(不使用yield return) IEnumerable<int> WithoutYield(List<int> list, int n) { List<int> result = new List<int>(); foreach (int i in list) { if (i > n) {

C#中,什么时候用yield return

yield关键字用于遍历循环中,yield return用于返回IEnumerable<T>,yield break用于终止循环遍历. 有这样的一个int类型的集合: static List<int> GetInitialData() { return new List<int>(){1,2,3,4}; } 需要打印出所有值大于2的元素. 不使用yield return的实现 static IEnumerable<int> FilterWithoutYiel