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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:20:44+00:00 2026-05-11T22:20:44+00:00

All of the examples I’ve seen of using yield return x; inside a C#

  • 0

All of the examples I’ve seen of using yield return x; inside a C# method could be done in the same way by just returning the whole list. In those cases, is there any benefit or advantage in using the yield return syntax vs. returning the list?

Also, in what types of scenarios would yield return be used that you couldn’t just return the complete list?

  • 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-11T22:20:44+00:00Added an answer on May 11, 2026 at 10:20 pm

    But what if you were building a collection yourself?

    In general, iterators can be used to lazily generate a sequence of objects. For example Enumerable.Range method does not have any kind of collection internally. It just generates the next number on demand. There are many uses to this lazy sequence generation using a state machine. Most of them are covered under functional programming concepts.

    In my opinion, if you are looking at iterators just as a way to enumerate through a collection (it’s just one of the simplest use cases), you’re going the wrong way. As I said, iterators are means for returning sequences. The sequence might even be infinite. There would be no way to return a list with infinite length and use the first 100 items. It has to be lazy sometimes. Returning a collection is considerably different from returning a collection generator (which is what an iterator is). It’s comparing apples to oranges.

    Hypothetical example:

    static IEnumerable<int> GetPrimeNumbers() {
       for (int num = 2; ; ++num) 
           if (IsPrime(num))
               yield return num;
    }
    
    static void Main() { 
       foreach (var i in GetPrimeNumbers()) 
           if (i < 10000)
               Console.WriteLine(i);
           else
               break;
    }
    

    This example prints prime numbers less than 10000. You can easily change it to print numbers less than a million without touching the prime number generation algorithm at all. In this example, you can’t return a list of all prime numbers because the sequence is infinite and the consumer doesn’t even know how many items it wants from the start.

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

Sidebar

Related Questions

In all examples online that I have seen, when rendering meta-balls (level sets of
Why do constants in all examples I've seen always start with k? And should
I have found that in all examples (include rails documentation) that I have seen
All the examples I see are for Java SE applications by passing your JAR
All the examples that I can search online use the App.Config mode of specifying
After freezing edge rails, all my controller examples are failing with MissingTemplate errors. e.g.,
Inspired by this question I began wondering why the following examples are all illegal
Can source code examples be kept in a SQL database while retaining all formatting
All examples I'm seeing show how to pull up entities matching a string exactly.
All examples in Qt show that one should use delegate classes to provide editors

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.