using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main (string[] args)
        {
            var test1 = Test1(1, 2);
            var test2 = Test2(3, 4);
        }

        static IEnumerable Test1(int v1, int v2)
        {
            yield break;
        }

        static IEnumerable Test2 (int v1, int v2)
        {
            return new String[] { };
        }
    }
}

"test1"似乎是一个综合与v1和v2(参数)作领域和"Test1"不叫。

"测试2"工作"设计":)

什么事?

有帮助吗?

解决方案

Test1 所谓,但是除非你迭代过来,你不会打断点上 yield break.

基本上 Test1 变成了一个国家机器,实现了 IEnumerable 你...但是所有的你的身体方法是在内部,国家机器,除非你 使用 国家机器的呼叫 GetEnumerator() 然后 MoveNext() (或者使用 foreach 环)你不会看到的你的身体执行。

看到我的 一般迭代的文章 和我的 迭代执行情况 文的详细信息,并还有两个埃里克利珀特的博客的员额: 心理调试的一部分心理调试的一部分的两个.

其他提示

既然你提到的Python,我要指出的是,在Python生成C#中工作得非常类似发电机。这里只有单独yield break可以改变一个C#方法到发生器的小的差别,而Python的等效raise StopIteration不会。

>>> def f():
...     print "Beginning of generator"
...     if False: yield
...     print "End of generator"
... 
>>> it = f()
>>> it
<generator object at 0x94fae2c>
>>> it.next()
Beginning of generator
End of generator
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top