How yield implements the pattern of lazy loading?

前端 未结 3 1267
清酒与你
清酒与你 2020-12-16 20:44

How yield implements the pattern of lazy loading?

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 21:11

    yield implementation doesn't reach the code until it's needed.

    For example, this code:

    public IEnumerable GetInts()
    {
        yield return 1;
        yield return 2;
        yield return 3;
    }
    

    Will actually compile into a nested class which implements IEnumerable and the body of GetInts() will return an instance of that class.

    Using reflector you can see:

    public IEnumerable GetInts()
    {
        d__6d d__d = new d__6d(-2);
        d__d.<>4__this = this;
        return d__d;
    }
    

    Edit - adding more info about GetInts implementation:
    The way this implementation makes it lazy is based on the Enumerator MoveNext() method. When the enumerable nested class is generated (d__6d in the example), it has a state and to each state a value is connected (this is a simple case, in more advanced cases the value will be evaluated when the code reach the state). If we take a look in the MoveNext() code of d__6d we'll see the state:

    private bool MoveNext()
    {
        switch (this.<>1__state)
        {
            case 0:
                this.<>1__state = -1;
                this.<>2__current = 1;
                this.<>1__state = 1;
                return true;
    
            case 1:
                this.<>1__state = -1;
                this.<>2__current = 2;
                this.<>1__state = 2;
                return true;
    
            case 2:
                this.<>1__state = -1;
                this.<>2__current = 3;
                this.<>1__state = 3;
                return true;
    
            case 3:
                this.<>1__state = -1;
                break;
        }
        return false;
    }
    

    When the enumerator is asked for the current object it returns the object which is connected to the current state.

    In order to show that the code is evaluated only when it's required you can look at this example:

    [TestFixture]
    public class YieldExample
    {
        private int flag = 0;
        public IEnumerable GetInts()
        {
            yield return 1;
            flag = 1;
            yield return 2;
            flag = 2;
            yield return 3;
            flag = 3;
        }
    
        [Test]
        public void Test()
        {
            int expectedFlag = 0;
            foreach (var i in GetInts())
            {
                Assert.That(flag, Is.EqualTo(expectedFlag));
                expectedFlag++;
            }
    
            Assert.That(flag, Is.EqualTo(expectedFlag));
        }
    }
    

    I hope it's a bit more clear. I recommend to take a look at the code with Reflector and observe the compiled code as you change the "yield" code.

提交回复
热议问题