Behind the scenes of the C # yield keyword,

Source: Internet
Author: User
Tags reflector

Behind the scenes of the C # yield keyword (),

Https://startbigthinksmall.wordpress.com/2008/06/09/behind-the-scenes-of-the-c-yield-keyword/

 

Behind the scenes of the C # yield keyword

June 9, 2008 by Lars Corneliussen

After reading the great article about the code-saving yield keyword "Give way to the yield keyword" by Shay Friedman I thought it cocould be interesting to know how the yield keyword works behind the scenes.

…it doesn’t really end the method’s execution. yield return pauses the method execution and the next time you call it (for the next enumeration value), the method will continue to execute from the last yield return call. It sounds a bit confusing I think… (ShayF)

By using yield return within a method that returnsIEnumerableOrIEnumeratorThe language feature is activated.

Note: IEnumerableIs kind of a stateless factory for Enumerators.IEnumerable. GetEnumerator ()Is thread safe and can be called multiple times, while the returned stateful Enumerator is just a helper for enumerating contained values once. By contractIEnumeratorOffers a Reset () method, but cannot implementations just throwNotSupportedException.

Lets create an enumerator method that yields some maid.

public class YieldingClass
{
    public IEnumerable<int> GetFibonachiSequence()
    {
        yield return 1;
        yield return 2;
        yield return 3;
        yield return 5;
    }
}

 

Note:Yield is not a feature of the. Net runtime. It is just a C # language feature which gets compiled into simple IL code by the C # compiler.

The compiler now generates a inner class with following signature (Reflector + some renaming ):

[CompilerGenerated]private sealed class YieldingEnumerator :   IEnumerable<object>, IEnumerator<object>{    // Fields    private int state;    private int current;    public YieldingClass owner;    private int initialThreadId;     // Methods    [DebuggerHidden]    public YieldingEnumerator(int state);    private bool MoveNext();    [DebuggerHidden]    IEnumerator<int> IEnumerable<int>.GetEnumerator();    [DebuggerHidden]    IEnumerator IEnumerable.GetEnumerator();    [DebuggerHidden]    void IEnumerator.Reset();    void IDisposable.Dispose();     // Properties    object IEnumerator<object>.Current    { [DebuggerHidden] get; }     object IEnumerator.Current    { [DebuggerHidden] get; }}

 

The original method GetFibonachiSequence () only returns a new instance of the YieldingEnumerator, passing the initial state-2 as well as itself as the owner.

Each enumerator holds a state indicating:

  • -2:Initialized as Enumerable. (Not yet an Enumerator)
  • -1:Closed
  • 0:Initialized as Enumerator.
    If a new Enumerator is requested on the same instance, GetEnumerator () returns another new instance of YieldingEnumerator.
  •  1-n:Index of the yield return in the original GetFibonachiSequence () method. In case of nested enumerators or other more complex scenarios one yield return consumes more than index.

The content of GetFibonachiSequence () is translated into YieldingEnumerator. MoveNext ().

In our very simple scenario the code looks like this:

 

[CompilerGenerated] private sealed class YieldingEnumerator :
   IEnumerable<object>, IEnumerator<object>
{
    // Fields
    private int state;
    private int current;
    public YieldingClass owner;
    private int initialThreadId;
 
    // Methods
    [DebuggerHidden]
    public YieldingEnumerator(int state);
    private bool MoveNext();
    [DebuggerHidden]
    IEnumerator<int> IEnumerable<int>.GetEnumerator();
    [DebuggerHidden]
    IEnumerator IEnumerable.GetEnumerator();
    [DebuggerHidden]
    void IEnumerator.Reset();
    void IDisposable.Dispose();
 
    // Properties
    object IEnumerator<object>.Current
    { [DebuggerHidden] get; }
 
    object IEnumerator.Current
    { [DebuggerHidden] get; }
}

 

Quite easy, isn' t it?

So far we easily cocould have created the classes and methods used to enable the yield keyword ourselves, too.

But in more complex scenarios Microsoft does some tricks, which won't compile as C #-at least not how Reflector translates the resulting IL code.

Lets have a look at some code with a nested enumeration...


bool MoveNext()
{
    switch (state)
    {
        case 0:
            state = -1;
            current = 1;
            state = 1;
            return true;
 
        case 1:
            state = -1;
            current = 2;
            state = 2;
            return true;
 
        case 2:
            state = -1;
            current = 3;
            state = 3;
            return true;
 
        case 3:
            state = -1;
            current = 5;
            state = 4;
            return true;
 
        case 4:
            state = -1;
            break;
    }
    return false;
}

 

This compiles:
private bool MoveNext()
{
    try
    {
        switch (state)
        {
            case 0:
                state = -1;
                state = 1;
                this.values = new int[] { 1, 2, 3, 5, 8 };
                this.currentPositionInValues = 0;
                while (this.currentPositionInValues < this.values.Length)
                {
                    current_i = this.values[this.currentPositionInValues];
                    current = current_i;
                    state = 2;
                    return true;
                Label_007F:
                    state = 1;
                    this.currentPositionInValues++;
                }
                this.Finally2();
                break;

            case 2:
                goto Label_007F;
        }
        return false;
    }
    fault
    {
        this.System.IDisposable.Dispose();
    }
}
[/sourcecode]

 

Now the states 1 and 2 are used to indicate whether the enumerator actually is at some point (2), or wether it is trying to retrieve the next value (1 ).

Two things wocould not compile:

  • Goto Label_007FIs used to jump back into the iteration overInt [] values. The C # goto statement is not able to jump into another statements context. But in IL this is totally valid asWhileStatement in MSIL is nothing but some gotos either.
  • The fault is proper MSIL, but not supported in C #. Basically it acts as a finally which just is executed in case of an error.

Attention:As in anonymous delegates, parameters as well as local and instance variables are passed to the YieldingEnumerator only once. Read this great post on this: Variable Scoping in Anonymous Delegates in C #

 

Thanks for your attention!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.