Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 54841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:17:02+00:00 2026-05-10T17:17:02+00:00

I’d love to figure it out myself but I was wondering roughly what’s the

  • 0

I’d love to figure it out myself but I was wondering roughly what’s the algorithm for converting a function with yield statements into a state machine for an enumerator? For example how does C# turn this:

IEnumerator<string> strings(IEnumerable<string> args)  { IEnumerator<string> enumerator2 = getAnotherEnumerator();         foreach(var arg in arg)      { enumerator2.MoveNext();       yield return arg+enumerator.Current;     }   } 

into this:

bool MoveNext()  { switch (this.state)     {         case 0:             this.state = -1;             this.enumerator2 = getAnotherEnumerator();             this.argsEnumerator = this.args.GetEnumerator();             this.state = 1;             while (this.argsEnumerator.MoveNext())             {                 this.arg = this.argsEnumerator.Current;                 this.enumerator2.MoveNext();                 this.current = this.arg + this.enumerator2.Current;                 this.state = 2;                 return true;                state1:                 this.state = 1;             }             this.state = -1;             if (this.argsEnumerator != null) this.argsEnumerator.Dispose();             break;          case 2:             goto state1;     }     return false; } 

Of course the result can be completely different depending on the original code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:17:02+00:00Added an answer on May 10, 2026 at 5:17 pm

    The particular code sample you are looking at involves a series of transformations. Please note that this is an approximate description of the algorithm. The actual names used by the compiler and the exact code it generates may be different. The idea is the same, however.

    The first transformation is the ‘foreach’ transformation, which transforms this code:

    foreach (var x in y) {    //body } 

    into this code:

    var enumerator = y.GetEnumerator(); while (enumerator.MoveNext()) {     var x = enumerator.Current;     //body }  if (y != null) {     enumerator.Dispose(); } 

    The second transformation finds all the yield return statements in the function body, assigns a number to each (a state value), and creates a ‘goto label’ right after the yield.

    The third transformation lifts all the local variables and function arguments in the method body into an object called a closure.

    Given the code in your example, that would look similar to this:

     class ClosureEnumerable : IEnumerable<string>  {     private IEnumerable<string> args;     private ClassType originalThis;     public ClosureEnumerator(ClassType origThis, IEnumerable<string> args)     {         this.args = args;         this.origianlThis = origThis;     }     public IEnumerator<string> GetEnumerator()     {         return new Closure(origThis, args);     }  }  class Closure : IEnumerator<string> {     public Closure(ClassType originalThis, IEnumerable<string> args)     {         state = 0;         this.args = args;         this.originalThis = originalThis;     }      private IEnumerable<string> args;     private IEnumerator<string> enumerator2;     private IEnumerator<string> argEnumerator;      //- Here ClassType is the type of the object that contained the method     //  This may be optimized away if the method does not access any      //  class members     private ClassType originalThis;      //This holds the state value.     private int state;     //The current value to return     private string currentValue;      public string Current     {         get          {             return currentValue;         }     } } 

    The method body is then moved from the original method to a method inside ‘Closure’ called MoveNext, which returns a bool, and implements IEnumerable.MoveNext. Any access to any locals is routed through ‘this’, and any access to any class members are routed through this.originalThis.

    Any ‘yield return expr’ is translated into:

    currentValue = expr; state = //the state number of the yield statement; return true; 

    Any yield break statement is translated into:

    state = -1; return false; 

    There is an ‘implicit’ yield break statement at the end of the function. A switch statement is then introduced at the beginning of the procedure that looks at the state number and jumps to the associated label.

    The original method is then translated into something like this:

    IEnumerator<string> strings(IEnumerable<string> args) {    return new ClosureEnumerable(this,args); } 

    The fact that the state of the method is all pushed into an object and that the MoveNext method uses a switch statement / state variable is what allows the iterator to behave as if control is being passed back to the point immediately after the last ‘yield return’ statement the next time ‘MoveNext’ is called.

    It is important to point out, however, that the transformation used by the C# compiler is not the best way to do this. It suffers from poor performance when trying to use ‘yield’ with recursive algorithms. There is a good paper that outlines a better way to do this here:

    http://research.microsoft.com/en-us/projects/specsharp/iterators.pdf

    It’s worth a read if you haven’t read it yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 51k
  • Answers 52k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The problem is that, for a non-matching string, your pattern… May 11, 2026 at 6:34 am
  • added an answer This should work: var count = $('#table tr').length; May 11, 2026 at 6:34 am
  • added an answer Look for a file called ASPNETDB.MDF in the App_Data folder… May 11, 2026 at 6:34 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.