1

I am getting an unexpected starting value for a list collection when I iterate through the list with the following code:

var lister = new List<int>();

for (int i = 0; i <= 300; i++)
{
    lister.Add(i);
}

foreach (var item in lister)
{
    Console.WriteLine(item);
}

The the first output to the console is 2 and not a 0 as expected. If I do this with an array, it works fine as expected. In other words, the output to the console starts with 0. Why is the for loop above not adding values to the lister collection object starting from 0, instead it starts adding values from the number 2 and on wards?

This problem does not happen when I iterate thorough an array with the following code:

var myArray = new int[300];

for (int i = 0; i < 300; i++)
{
    myArray[i] = i;
}

foreach (var item in myArray)
{
    Console.WriteLine(item);
}
1
  • thats a typo. list should be lister. I fixed the error in the post. thanks. I'm still in the dark on this. I have no idea why this is happening. Commented Apr 19, 2014 at 0:21

2 Answers 2

2

Selman22's answer is the correct reason for what you're seeing - the console by default has a buffer of 300 lines. Everything earlier than the last 300 lines is scrolled off the console window and lost.

You can change the buffer, increasing it to whatever size you need:

Console.BufferHeight = 9999;

...

// now you'll be able to scroll up and see all your numbers
foreach (var item in lister)
{
    Console.WriteLine(item);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Instead of hard coding it into the source code, you can change the buffer height of the console by right clicking on its title bar, clicking on the layout tab, and then changing the value for the height field. Thanks for your solution as well.
@GrantWinney indeed, I was always using the Console.Read to limit displaying results in console, I don't play with Console very often,so anyway this is good to know, everyday we learn something new :)
I don't have the problem of values being cut off when I iterate through an array. Why is this problem happening with list objects?
@GrantWinney I have updated my original question with that code.
2

Actually it is displaying the 0 but you can see it because your console size is not big enough.Try:

foreach (var item in lister)
{
    Console.WriteLine(item);
    if(item % 100 == 0) Console.Read();
}

And press enter to see next 100 numbers.

3 Comments

actually i was able to scroll to the top of the list in the output screen and sure enough it started from 2 not 0 as expected. And yes it was a typo I fixed it. thanks.
I know, but that's not what I mean, 0 and 1 are displayed then they disappearing, for ex. you can change 300 to 1000 and you will see it will start from 700 or 701,702.
you are right, but I still don't see 0 and 1 even after maximizing the console window. I changed the max value from 300 to 30 and it was displaying from 0 and 1 without having to maximize the console window. How do I solve this problem? thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.