产量返回工作模式 [英] working pattern of yield return

查看:99
本文介绍了产量返回工作模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有一个代码块

static void Main()
{

  foreach (int i in YieldDemo.SupplyIntegers())
  {
    Console.WriteLine("{0} is consumed by foreach iteration", i);
  }
}


 class YieldDemo
  {
    public static IEnumerable<int> SupplyIntegers()
     {
         yield return 1;
         yield return 2;
          yield return 3;
       }
   }



我可以解释背后收益回报的原则

can i interpret the principle behind yield return as


  1. main()调用的SupplyIntegers()

  2. | 1 | | 2 | | 3 |存储在IEnumerator的的连续内存block.Pointer移至| 1 |

  3. 从SupplyInteger()main()的控制返回

  4. main()的输出值

  5. 指针移动到| 2 |,等等

  1. Main() calls the SupplyIntegers()
  2. |1| |2| |3| are stored in contiguous memory block.Pointer of "IEnumerator" Moves to |1|
  3. Control returns from SupplyInteger() to Main().
  4. Main() prints the value
  5. Pointer Moves to |2|, and so on.

澄清:

(1)一般情况下,我们将有一个有效的return语句是允许一个function.How内的确
C#治疗时多产量的回报,回报收益率,...陈述存在?

(1) Normally we will have one valid return statement is allowed inside a function.How does C# treats when multiple yield return ,yield return,... statements are present?

(2)一旦遇到退货也没有办法了控制再回来
SupplyIntegers(),如果它被允许不会屈服再次从1开始?我的意思是产生收益1

(2) Once the return is encountered there is no way for control again coming back to SupplyIntegers(), in case it is allowed won't the Yield again starts from 1 ? I mean yield return 1?

推荐答案

都能跟得上 - 远离它?;我会写一个长手版本你...实在是太蹩脚!

Nope - far from it; I'll write a long-hand version for you... it is too grungy!


请注意它也帮助,如果你明白,的foreach 实际上是:

Note it also helps if you understand that the foreach is actually:

using(var iterator = YieldDemo.SupplyIntegers().GetEnumerator()) {
    int i;
    while(iterator.MoveNext()) {
        i = iterator.Current;
         Console.WriteLine("{0} is consumed by foreach iteration", i);
    }
}





using System;
using System.Collections;
using System.Collections.Generic;
static class Program
{
    static void Main()
    {

        foreach (int i in YieldDemo.SupplyIntegers())
        {
            Console.WriteLine("{0} is consumed by foreach iteration", i);
        }
    }
}

 class YieldDemo
  {

    public static IEnumerable<int> SupplyIntegers()
     {
         return new YieldEnumerable();
       }
    class YieldEnumerable : IEnumerable<int>
    {
        public IEnumerator<int> GetEnumerator()
        {
            return new YieldIterator();
        }
        IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
    }
    class YieldIterator : IEnumerator<int>
    {
        private int state = 0;
        private int value;
        public int Current { get { return value; } }
        object IEnumerator.Current { get { return Current; } }
        void IEnumerator.Reset() { throw new NotSupportedException(); }
        void IDisposable.Dispose() { }
        public bool MoveNext()
        {
            switch (state)
            {
                case 0: value = 1; state = 1;  return true;
                case 1: value = 2; state = 2;  return true;
                case 2: value = 3; state = 3; return true;
                default: return false;
            }
        }
    }
}



由于你可以看到,它建立了一个状态机的迭代器,由的MoveNext 进展状态机。我已经使用了状态字段的模式,你可以看到这是如何为更复杂的迭代工作。

As you can see, it builds a state machine in the iterator, with the state machine progressed by MoveNext. I've used the pattern with a state field, as you can see how this would work for more complex iterators.

重要:


  • 在迭代器块的任何变量成为状态机上的字段

  • 如果你有一个最后块(包括使用),它会在的Dispose()

  • 导致收益率的收益部分代码变成了情况(大约)

  • 产量突破变成了状态= -1;返回false; (或类似)

  • any variables in your iterator block become fields on the state machine
  • if you have a finally block (including using), it goes in the Dispose()
  • portions of code leading to a yield return become a case (roughly)
  • yield break becomes a state = -1; return false; (or similar)

C#编译器确实这是很复杂的方式,但它使得编写迭代器变得轻而易举。

The way the C# compiler does this is very complicated, but it makes writing iterators a breeze.

这篇关于产量返回工作模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆