Interesting use of the C# yield keyword in Nerd Dinner tutorial

后端 未结 3 1181
不思量自难忘°
不思量自难忘° 2021-02-02 17:35

Working through a tutorial (Professional ASP.NET MVC - Nerd Dinner), I came across this snippet of code:

public IEnumerable GetRuleViolation         


        
相关标签:
3条回答
  • Short version:

    1: The yield is the magic "Stop and come back later" keyword, so the if statements in front of the "active" one have been evaluated.

    2: yield break explicitly ends the enumeration (think "break" in a switch case)

    3: Every time. You can cache the result, of course, by turning it into a List for example and iterating over that afterwards.

    0 讨论(0)
  • 2021-02-02 17:55

    1) Take this simpler example:

    public void Enumerate()
    {
        foreach (var item in EnumerateItems())
        {
            Console.WriteLine(item);
        }
    }
    
    public IEnumerable<string> EnumerateItems()
    {
        yield return "item1";
        yield return "item2";
        yield break;
    }
    

    Each time you call MoveNext() from the IEnumerator the code returns from the yield point and moves to the next executable line of code.

    2) yield break; will tell the IEnumerator that there is nothing more to enumerate.

    3) once per enumeration.

    Using yield break;

    public IEnumerable<string> EnumerateUntilEmpty()
    {
        foreach (var name in nameList)
        {
            if (String.IsNullOrEmpty(name)) yield break;
            yield return name;
        }     
    }
    
    0 讨论(0)
  • 2021-02-02 18:02

    A function that contains yield commands is treated differently than a normal function. What is happening behind the scenes when that function is called, is that an anonymous type is constructed of the specific IEnumerable type of the function, the function creates an object of that type and returns it. The anonymous class contains logic that executes the body of the function up until the next yield command for every time the IEnumerable.MoveNext is called. It is a bit misleading, the body of the function is not executed in one batch like a normal function, but rather in pieces, each piece executes when the enumerator moves one step forward.

    With regards to your questions:

    1. As I said, each if gets executed when you iterate to the next element.
    2. yield break is indeed not necessary in the example above. What it does is it terminates the enumeration.
    3. Each time you iterate over the enumerable, you force the execution of the code again. Put a breakpoint on the relevant line and test for yourself.
    0 讨论(0)
提交回复
热议问题