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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:19:16+00:00 2026-05-27T00:19:16+00:00

Take a guess … how long will this program take to produce the very

  • 0

Take a guess … how long will this program take to produce the very first output when i == 0? It should be instant, right? And through lazy evaluation of the yield it should produce output in rapid succession after that, right?

static void Main(string[] args)
{
   Stopwatch stopwatch = Stopwatch.StartNew();
   int i = 0;
   foreach (var item in massiveYieldStatement())
   {
        if (i++ % 10000 == 0) 
           Console.WriteLine(stopwatch.ElapsedMilliseconds / 1000);
   }
   Console.ReadKey();
}

static IEnumerable<string> massiveYieldStatement()
{
   yield return "a"; 
   yield return "a";

   .. repeat 200,000 times !!

   yield return "a";
}

But it doesn’t! It sits there with no output for between 4 and 21 minutes and then completes quickly – under 60ms in one case! During those minutes, 100% of one core’s worth of CPU is used and memory usage grows. In the actual scenario where I encountered this a Stackoverflow exception is thrown before the first iteration even happens! I have tried it in debug mode in Visual Studio and in release mode from the command prompt. I have tried it on Windows 7 x64 and Windows Server 2008 R2 x64.

Can anyone explain what’s happening here? Does it repro for you?

NOTE: This is not the real code: the real code has far fewer yield statements but is much more complex. This is just the simplest repro.

  • 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-27T00:19:17+00:00Added an answer on May 27, 2026 at 12:19 am

    The assembly produced by this code is a few MB large. yield return is a special beast, in that it looks deceptively simple but the C# compiler actually generates a class (‘state machine’) to implement the massiveYieldStatement method. I’m pretty sure that you’re waiting for the JIT compiler to compile the MoveNext() method of that class (you can verify that with ildasm: if you try to open the MoveNext() method it takes a lot of time as well).

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

Sidebar

Related Questions

My question is very similar this but I guess I need to take it
Well Im stucked at this point.please take a look at this http://jsfiddle.net/karthik64/5jhgF/3/ Okay first
Take this code: <?php if (isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; } if
Take the following string as an example: The quick brown fox Right now the
Take this non-compiling code for instance: public string GetPath(string basefolder, string[] extraFolders) { string
Here's my code: #This is a game to guess a random number. import random
Take a look at this example code, which doesn't work: <?xml version='1.0' encoding='UTF-8' ?>
So I guess this isn't technically a code question, but it's something that I'm
I guess the solution for this is quite simple, but I've been thinking about
I guess this is not quite a programming question, but it is bothering me

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.