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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:56:55+00:00 2026-05-11T00:56:55+00:00

still trying to find where i would use the yield keyword in a real

  • 0

still trying to find where i would use the ‘yield’ keyword in a real situation.

I see this thread on the subject

What is the yield keyword used for in C#?

but in the accepted answer, they have this as an example where someone is iterating around Integers()

public IEnumerable<int> Integers() { yield return 1; yield return 2; yield return 4; yield return 8; yield return 16; yield return 16777216; } 

but why not just use

list<int> 

here instead. seems more straightforward..

  • 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. 2026-05-11T00:56:56+00:00Added an answer on May 11, 2026 at 12:56 am

    If you build and return a List (say it has 1 million elements), that’s a big chunk of memory, and also of work to create it.

    Sometimes the caller may only want to know what the first element is. Or they might want to write them to a file as they get them, rather than building the whole list in memory and then writing it to a file.

    That’s why it makes more sense to use yield return. It doesn’t look that different to building the whole list and returning it, but it’s very different because the whole list doesn’t have to be created in memory before the caller can look at the first item on it.

    When the caller says:

    foreach (int i in Integers()) {    // do something with i } 

    Each time the loop requires a new i, it runs a bit more of the code in Integers(). The code in that function is ‘paused’ when it hits a yield return statement.

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

Sidebar

Related Questions

I'm trying to find out if a specific MySQL User is still in use
I'm trying to find a good way to handle the following scenario (I'm still
Ok Im still new to Zend, and trying to find how the ORM works
Still trying to figure this out from yesterday. I am trying to scroll an
I'm building a small application in ASP.NET MVC. I'm still trying to find my
I'm trying to find out more about MySQL VIEW's, and I'm still quite confused
This question is part user experience, part engineering. I am trying to find a
I love LINQPad and use it daily. I have been trying to find a
G'day, I'm trying to find a way to use a vector of [x,y] points
I'm trying to find documentations on how to setup Paperclip to use fog.io and

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.