What does “yield break;” do in C#?

后端 未结 8 1896
南方客
南方客 2020-11-28 01:04

I have seen this syntax in MSDN: yield break, but I don\'t know what it does. Does anyone know?

相关标签:
8条回答
  • 2020-11-28 01:18

    It specifies that an iterator has come to an end. You can think of yield break as a return statement which does not return a value.

    For example, if you define a function as an iterator, the body of the function may look like this:

    for (int i = 0; i < 5; i++)
    {
        yield return i;
    }
    
    Console.Out.WriteLine("You will see me");
    

    Note that after the loop has completed all its cycles, the last line gets executed and you will see the message in your console app.

    Or like this with 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");
    

    In this case the last statement is never executed because we left the function early.

    0 讨论(0)
  • 2020-11-28 01:20

    Ends an iterator block (e.g. says there are no more elements in the IEnumerable).

    0 讨论(0)
  • 2020-11-28 01:20

    The yield break statement causes the enumeration to stop. In effect, yield break completes the enumeration without returning any additional items.

    Consider that there are actually two ways that an iterator method could stop iterating. In one case, the logic of the method could naturally exit the method after returning all the items. Here is an example:

    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.");
    }
    

    In the above example, the iterator method will naturally stop executing once maxCount primes have been found.

    The yield break statement is another way for the iterator to cease enumerating. It is a way to break out of the enumeration early. Here is the same method as above. This time, the method has a limit on the amount of time that the method can execute.

    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.");
    }
    

    Notice the call to yield break. In effect, it is exiting the enumeration early.

    Notice too that the yield break works differently than just a plain break. In the above example, yield break exits the method without making the call to Debug.WriteLine(..).

    0 讨论(0)
  • 2020-11-28 01:20

    yield break is just a way of saying return for the last time and don't return any value

    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;
     }
    
    0 讨论(0)
  • 2020-11-28 01:21

    Tells the iterator that it's reached the end.

    As an example:

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 01:22

    The yield keyword is used together with the return keyword to provide a value to the enumerator object. yield return specifies the value, or values, returned. When the yield return statement is reached, the current location is stored. Execution is restarted from this location the next time the iterator is called.

    To explain the meaning using an example:

        public IEnumerable<int> SampleNumbers()
        {
            int counter = 0;
            yield return counter;
    
            counter = counter + 2;
    
            yield return counter;
    
            counter = counter + 3;
    
            yield return counter ;
        }
    

    Values returned when this is iterated are: 0, 2, 5.

    It’s important to note that counter variable in this example is a local variable. After the second iteration which returns the value of 2, third iteration starts from where it left before, while preserving the previous value of local variable named counter which was 2.

    0 讨论(0)
提交回复
热议问题