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


当前回答

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

举个例子:

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中没有更多的元素)。

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

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;
    }
}

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。

它指定迭代器已经结束。您可以将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");

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