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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:29:33+00:00 2026-05-11T19:29:33+00:00

On this article ( http://blogs.msdn.com/oldnewthing/archive/2008/08/13/8854601.aspx ), there is a pop question about iterators and

  • 0

On this article ( http://blogs.msdn.com/oldnewthing/archive/2008/08/13/8854601.aspx), there is a pop question about iterators and one concerning a corner case:

Exercise: Consider the following
fragment: foreach (int i in
CountTo100Twice()) { … }

Explain what happens on the 150th call
to MoveNext() in the above loop.
Discuss its consequences for recursive
enumerators (such as tree traversal).

I haven’t run this code, but I am assuming that this is a question supposedly with an answer from the articles (all links provided below), but I can’t find the answer in the knowledge shared by the article or in the comments for this particular article.

Does anyone know what the answer is? What other corner cases are there?

1) http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx

2) http://blogs.msdn.com/oldnewthing/archive/2008/08/13/8854601.aspx

3) http://blogs.msdn.com/oldnewthing/archive/2008/08/14/8862242.aspx

4) http://blogs.msdn.com/oldnewthing/archive/2008/08/15/8868267.aspx

Thanks

  • 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-11T19:29:33+00:00Added an answer on May 11, 2026 at 7:29 pm

    I suspect he may be referring to the fact that each call to MoveNext() invokes a state machine which in turn invokes MoveNext() on another state machine, making it all a bit inefficient.

    This is blogged about here by Wes Dyer and here by Eric Lippert.

    The main corner case I’d say with iterator blocks is that nothing is executed before the first call to MoveNext(). So this method:

    public IEnumerable<string> ReadLines(string file)
    {
        if (file == null)
        {
            throw new ArgumentNullException("file");
        }
        using (TextReader reader = File.OpenText(file))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }        
    }
    

    doesn’t actually throw the exception until you start iterating. Instead, you need to write something like this:

    public static IEnumerable<string> ReadLines(string file)
    {
        if (file == null)
        {
            throw new ArgumentNullException("file");
        }
        return ReadLinesImpl(file);
    }
    
    public static IEnumerable<string> ReadLinesImpl(string file)
    {
        using (TextReader reader = File.OpenText(file))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }        
    }
    

    Again, Eric has blogged on this: part 1, part 2. I’ve blogged a suggestion to make life easier too, although I doubt it’ll ever come to anything.

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

Sidebar

Related Questions

In C following this article ( http://blogs.msdn.com/oldnewthing/archive/2006/12/21/1340571.aspx ), we have succesfully been able to
I am looking at this article on monads: http://blogs.msdn.com/b/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx I am writing the code
My question is partially inspired by this article written by Eric Lippert: http://blogs.msdn.com/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx Using
I am following this article (http://blogs.msdn.com/b/kaevans/archive/2011/04/11/intro-to-windbg-for-net-developers.aspx )and it is saying download symbols from Microsoft
From this article http://blogs.msdn.com/b/ericlippert/archive/2010/04/08/precision-and-accuracy-of-datetime.aspx : Now, the question how much time has elapsed from
i m using this article http://blogs.msdn.com/delay/archive/2009/09/23/if-it-walks-like-a-duck-and-talks-like-a-duck-it-must-be-a-treegrid-a-simple-xaml-only-treegrid-ui-for-wpf.aspx to have hierarchical data... i am having treeview
Recently I came to know from this article - http://blogs.msdn.com/b/patrickdanino/archive/2009/11/11/custom-controls-and-ui-automation.aspx -that controls in WPF
I was reading the following article http://blogs.msdn.com/b/wenlong/archive/2008/08/13/orcas-sp1-improvement-asynchronous-wcf-http-module-handler-for-iis7-for-better-server-scalability.aspx and I am a little confused. First
I was watching this David's article. http://blogs.msdn.com/b/dphill/archive/2011/01/23/closable-tabbed-views-in-prism.aspx He's using blend interacvity. I want to
This blog http://blogs.msdn.com/sqlserverstorageengine/archive/2009/01/04/managing-tempdb-in-sql-server-tempdb-configuration.aspx states that it is a good idea to Spread TempDB across

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.