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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:41:15+00:00 2026-05-28T15:41:15+00:00

Currently I’m reading a collection of items from a stream. I do this as

  • 0

Currently I’m reading a collection of items from a stream. I do this as following:

public class Parser{

 private TextReader _reader; //Get set in Constructor
 private IEnumerable<Item> _items;    

 public IEnumerable<Item> Items{
  get{
   //I >>thought<< this would prevent LoadItems() from being called twice.
   return _items ?? (_items = LoadItems());
  }
 }

 public IEnumerable<Item> LoadItems(){
  while(_reader.Peek() >= 0){
   yield return new Item(_reader.ReadLine()); //Actually it's a little different
  }
 }
}

Let say I have a stream which contains two items, and I do the following:

var textReader = //Load textreader here
var parser = new Parser(textReader);
var result1 = parser.Items.Count();
var result2 = parser.Items.Count();

Now result1 is 2, while result2 is one.

Now I noticed, that my null check is useless? It seems that everytime I call that function it gets yielded anyway.

Can someone explain to me why this is? And what would be the best solution for this situation (please tell me if what I’m doing is complete crap :P).

  • 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-28T15:41:16+00:00Added an answer on May 28, 2026 at 3:41 pm

    Because LoadItems is a lazy enumerable (uses yield) and you are assigning it to a field, it means that every time you enumerate _items you are actually causing the loop within LoadItems() to be run again, i.e. (Enumerable.Count creates a new Enumerator each time which cause the LoadItems body to be run again). As you are not creating the reader afresh each time within LoadItems its cursor will be positioned at the end of the stream so will likely not be able to read any more lines — I suspect that it is returning null and your your single Item object returned on the second call contains a null string.

    Solutions to this would be to ‘realise’ the result of LoadItems by calling Enumerable.ToList which will give you a concrete list:

    return _items ?? (_items = LoadItems().ToList());
    

    Or seeking the reader back to the beginning of the stream (if possible) such that LoadItems can run again each time identically.

    But I would recommend you simply get rid of the yielding in this case and return a concrete list as there is little benefit so you are paying the complexity price for no gain.

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

Sidebar

Related Questions

Currently I am adding object by creating it like: type TRecord = class private
Currently my WPF application imports a part like this [Import(typeof(ILedPanel)] public ILedPanel Panel {
Currently my program generates random 8 character strings made from numbers. See below public
Currently I'm doing some unit tests which are executed from bash. Unit tests are
Currently I am debugging the signing of an Android app. And this would be
Currently, I have: <html> <div class=yes1><span><img src=img></span></div> </html> <script> var x = ='yes1' var
Currently have the following expression: @[TMS_Dest] + \\ + @[TMS_Rename] + (DT_WSTR,4)DatePart(yyyy, GetDate()) +
Currently, my MVC 3 app has a dependency on a static class that is
Currently I have the following Mapping in my Controller: @RequestMapping( value = /add.html, method
Currently, I use an abstract factory to allow the specification of a custom class

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.