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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:39:37+00:00 2026-05-14T15:39:37+00:00

A trivial example of an infinite IEnumerable would be IEnumerable<int> Numbers() { int i=0;

  • 0

A trivial example of an “infinite” IEnumerable would be

IEnumerable<int> Numbers() {
  int i=0;
  while(true) {
    yield return unchecked(i++);
  }
}

I know, that

foreach(int i in Numbers().Take(10)) {
  Console.WriteLine(i);
}

and

var q = Numbers();
foreach(int i in q.Take(10)) {
  Console.WriteLine(i);
}

both work fine (and print out the number 0-9).

But are there any pitfalls when copying or handling expressions like q? Can I rely on the fact, that they are always evaluated “lazy”? Is there any danger to produce an infinite loop?

  • 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-14T15:39:38+00:00Added an answer on May 14, 2026 at 3:39 pm

    Yes, you are guaranteed that the code above will be executed lazily. While it looks (in your code) like you’d loop forever, your code actually produces something like this:

    IEnumerable<int> Numbers()
    {
        return new PrivateNumbersEnumerable();
    }
    
    private class PrivateNumbersEnumerable : IEnumerable<int>
    {
        public IEnumerator<int> GetEnumerator() 
        { 
            return new PrivateNumbersEnumerator(); 
        }
    }
    
    private class PrivateNumbersEnumerator : IEnumerator<int>
    {
        private int i;
    
        public bool MoveNext() { i++; return true; }   
    
        public int Current
        {
            get { return i; }
        }
    }
    

    (This obviously isn’t exactly what will be generated, since this is pretty specific to your code, but it’s nonetheless similar and should show you why it’s going to be lazily evaluated).

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

Sidebar

Related Questions

I have a class that needs access to urllib2, the trivial example for me
Consider this contrived, trivial example: var foo = new byte[] {246, 127}; var bar
A pretty silly trivial question. The canonical example is f = open('filename') , but
Probably a very trivial problem. I have an object that looks like this: @PersistenceCapable
Consider this trivial example of fork() ing then waiting for a child to die
See http://jsfiddle.net/FDhQF/1/ for a trivial example. What's the difference between something being undefined and
Now that I know how to parse xml in scala as a stream I
The trivial solution would be: class Number { public: bool isFinite(); bool isPositive(); double
What are some clever uses for infinite generators? I've seen lots of seemingly trivial
I've read the RebaseProject page and tried a non-trivial example (not rebasing a complete

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.