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 9284661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:57:40+00:00 2026-06-18T18:57:40+00:00

I am using yield and struck somewhere , can anyone explain how yield work

  • 0

I am using yield and struck somewhere , can anyone explain how yield work
my scenerio is shown below.

public static IEnumerable Power(int number, int exponent)
{
    int result = 1;
    int counter = 0;
    Console.WriteLine("Inside Power - Before While");
    while (counter++ < exponent)
    {
        Console.WriteLine("Inside Power - Inside While");
        result = result * number;
        yield return result;
        //Console.WriteLine("New line added");
    }
    Console.WriteLine("Inside Power - After While"); 
}

static void Main(string[] args)
{
    foreach (int i in Power(2, 8))
    {
        Console.WriteLine("{0}", i);
    }
}

So the output we are getting here is

Inside Power - Before While
Inside power - Inside While
2
Inside power - Inside While
4
Inside power - Inside While
8
Inside power - Inside While
16
Inside power - Inside While
32
Inside power - Inside While
64
Inside power - Inside While
128
Inside power - Inside While
256
Inside power - AfterWhile

So my question is how the pointer shifts from foreach to Enumerable method while loop and prints and so on.
why whole method is not called and only while loop is executing each time.

  • 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-06-18T18:57:41+00:00Added an answer on June 18, 2026 at 6:57 pm

    The yield return statement is semantically equivalent to a return statement (which passes control flow to the calling method), followed by a “goto” to the yield statement in the next iteration of the foreach loop.

    Return
    Goto
    

    This behavior does not exist in the Common Language Runtime. It is implemented by a class generated by the C# compiler. This is then executed and JIT-compiled by the CLR. Yield is a form of syntactic sugar.

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

Sidebar

Related Questions

The code below works but I want to optimize the code using yield or
I've heard that using el.innerText||el.textContent can yield unreliable cross-browswer results, so I'm walking the
In the tests below, I cannot get Console.WriteLine to really print when using yield
All of the examples I've seen of using yield return x; inside a C#
When you write an iterator function in C#, using yield syntax, the compiler internally
Using Google + Bing didn't yield an answer to what should be a simple
i'm using ECLiPSe programming logic system. i want to implement the yield method for
Preface: I'm know that in most cases using a volatile field won't yield any
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Besides just using yield for iterators in Ruby, I also use it to pass

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.