我在MSDN中见过这种语法:yield break,但我不知道它是做什么的。有人知道吗?


当前回答

http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/就是一个很好的例子:

public static IEnumerable<int> Range( int min, int max )
{
   while ( true )
   {
      if ( min >= max )
      {
         yield break;
      }
      yield return min++;
   }
}

解释一下,如果yield break语句在一个方法中命中,该方法的执行将停止,不返回。在某些情况下,当你不想给出任何结果时,你可以使用yield break。

其他回答

收益率突破只是一种表示最后一次返回并且不返回任何值的方式

e.g

// returns 1,2,3,4,5
IEnumerable<int> CountToFive()
{
    yield return 1;
    yield return 2;
    yield return 3;
    yield return 4;
    yield return 5;
    yield break;
    yield return 6;
    yield return 7;
    yield return 8;
    yield return 9;
 }

告诉迭代器它已经到达终点。

举个例子:

public interface INode
{
    IEnumerable<Node> GetChildren();
}

public class NodeWithTenChildren : INode
{
    private Node[] m_children = new Node[10];

    public IEnumerable<Node> GetChildren()
    {
        for( int n = 0; n < 10; ++n )
        {
            yield return m_children[ n ];
        }
    }
}

public class NodeWithNoChildren : INode
{
    public IEnumerable<Node> GetChildren()
    {
        yield break;
    }
}

结束迭代器块(例如,表示IEnumerable中没有更多的元素)。

它指定迭代器已经结束。您可以将yield break看作一个不返回值的return语句。

例如,如果你将一个函数定义为迭代器,函数体可能是这样的:

for (int i = 0; i < 5; i++)
{
    yield return i;
}

Console.Out.WriteLine("You will see me");

注意,在循环完成所有循环后,最后一行执行,您将在控制台应用程序中看到消息。

或者像这样的yield break:

int i = 0;
while (true)
{
    if (i < 5)
    {
        yield return i;
    }
    else
    {
        // note that i++ will not be executed after this
        yield break;
    }
    i++;
}

Console.Out.WriteLine("Won't see me");

在这种情况下,最后一条语句永远不会执行,因为我们提前离开了函数。

yield break语句导致枚举停止。实际上,yield break完成了枚举,而不返回任何附加项。

考虑到迭代器方法实际上有两种停止迭代的方式。在一种情况下,方法的逻辑可以在返回所有项后自然退出方法。这里有一个例子:

IEnumerable<uint> FindPrimes(uint startAt, uint maxCount)
{
    for (var i = 0UL; i < maxCount; i++)
    {
        startAt = NextPrime(startAt);
        yield return startAt;
    }

    Debug.WriteLine("All the primes were found.");
}

在上面的例子中,一旦找到maxCount质数,迭代器方法自然会停止执行。

yield break语句是迭代器停止枚举的另一种方式。这是一种提前跳出枚举的方法。这里是与上面相同的方法。这一次,该方法对该方法可以执行的时间有一个限制。

IEnumerable<uint> FindPrimes(uint startAt, uint maxCount, int maxMinutes)
{
    var sw = System.Diagnostics.Stopwatch.StartNew();
    for (var i = 0UL; i < maxCount; i++)
    {
        startAt = NextPrime(startAt);
        yield return startAt;

        if (sw.Elapsed.TotalMinutes > maxMinutes)
            yield break;
    }

    Debug.WriteLine("All the primes were found.");
}

注意yield break的调用。实际上,它提早退出枚举。

还要注意,yield break与普通break的工作方式不同。在上面的例子中,yield break退出方法而不调用Debug.WriteLine(..)。