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) yield return null;
                result = result * num;
                yield return result;
            }
        }
        public static IEnumerator Power2(int num, int exponent)
        {
            int counter = 0;
            int result = 1;
            while (counter++ < exponent)
            {
                //if (counter == 4) yield break;
                if (counter == 4) yield return null;
                result = result * num;
                yield return result;
            }
        }
        static void Main(string[] args)
        {
            foreach (var item in Power(2,8))
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("---");
            var e = Power2(2, 8);
            while (e.MoveNext())
            {
                var i = e.Current;
                Console.WriteLine(i);
            }

            Console.WriteLine("main done");
            Console.ReadKey();
        }
    }
}

  

原文地址:https://www.cnblogs.com/wucg/p/9189342.html

时间: 2024-08-30 01:35:36

C# yield return; yield break;的相关文章

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()    {       

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