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

  • Home
  • SEARCH
  • 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 424271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:15:48+00:00 2026-05-12T19:15:48+00:00

When i have a code block static void Main() { foreach (int i in

  • 0

When i have a code block

static void Main()
{

  foreach (int i in YieldDemo.SupplyIntegers())
  {
    Console.WriteLine("{0} is consumed by foreach iteration", i);
  }
}


 class YieldDemo
  {
    public static IEnumerable<int> SupplyIntegers()
     {
         yield return 1;
         yield return 2;
          yield return 3;
       }
   }

can i interpret the principle behind yield return as

  1. Main() calls the SupplyIntegers()
  2. |1| |2| |3| are stored in contiguous memory block.Pointer of "IEnumerator" Moves to |1|
  3. Control returns from SupplyInteger() to Main().
  4. Main() prints the value
  5. Pointer Moves to |2|, and so on.

Clarifications :

(1) Normally we will have one valid return statement is allowed inside a function.How does
C# treats when multiple yield return ,yield return,… statements are present?

(2) Once the return is encountered there is no way for control again coming back to
SupplyIntegers(), in case it is allowed won’t the Yield again starts from 1 ? I mean yield return 1?

  • 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. Editorial Team
    Editorial Team
    2026-05-12T19:15:48+00:00Added an answer on May 12, 2026 at 7:15 pm

    Nope – far from it; I’ll write a long-hand version for you… it is too grungy!


    Note it also helps if you understand that the foreach is actually:

    using(var iterator = YieldDemo.SupplyIntegers().GetEnumerator()) {
        int i;
        while(iterator.MoveNext()) {
            i = iterator.Current;
             Console.WriteLine("{0} is consumed by foreach iteration", i);
        }
    }
    

    using System;
    using System.Collections;
    using System.Collections.Generic;
    static class Program
    {
        static void Main()
        {
    
            foreach (int i in YieldDemo.SupplyIntegers())
            {
                Console.WriteLine("{0} is consumed by foreach iteration", i);
            }
        }
    }
    
     class YieldDemo
      {
    
        public static IEnumerable<int> SupplyIntegers()
         {
             return new YieldEnumerable();
           }
        class YieldEnumerable : IEnumerable<int>
        {
            public IEnumerator<int> GetEnumerator()
            {
                return new YieldIterator();
            }
            IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
        }
        class YieldIterator : IEnumerator<int>
        {
            private int state = 0;
            private int value;
            public int Current { get { return value; } }
            object IEnumerator.Current { get { return Current; } }
            void IEnumerator.Reset() { throw new NotSupportedException(); }
            void IDisposable.Dispose() { }
            public bool MoveNext()
            {
                switch (state)
                {
                    case 0: value = 1; state = 1;  return true;
                    case 1: value = 2; state = 2;  return true;
                    case 2: value = 3; state = 3; return true;
                    default: return false;
                }
            }
        }
    }
    

    As you can see, it builds a state machine in the iterator, with the state machine progressed by MoveNext. I’ve used the pattern with a state field, as you can see how this would work for more complex iterators.

    Importantly:

    • any variables in your iterator block become fields on the state machine
    • if you have a finally block (including using), it goes in the Dispose()
    • portions of code leading to a yield return become a case (roughly)
    • yield break becomes a state = -1; return false; (or similar)

    The way the C# compiler does this is very complicated, but it makes writing iterators a breeze.

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

Sidebar

Ask A Question

Stats

  • Questions 211k
  • Answers 211k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This seems to do what I want but it's not… May 12, 2026 at 10:12 pm
  • Editorial Team
    Editorial Team added an answer This should get you started: var sortedGridData = rawGridData.OrderBy(r =>… May 12, 2026 at 10:12 pm
  • Editorial Team
    Editorial Team added an answer One alternative that I've found is C# Webserver on CodePlex.… May 12, 2026 at 10:12 pm

Related Questions

I have a Java class that looks like this: public class My_ABC { int
following is the code listed in MainClass.java. public class MainClass { public static void
I have a .Net service that has begun to refuse to start - the
So, I have a list containing a custom class, MyClass MyClass has properties, which

Trending Tags

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

Top Members

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.