Reputation: 13
I am attempting to learn to program in c# from some online courses in order to help round out my knowledge. It is proving to be extremely challenging. I seem to think I understand, and then, when I sit down to work through the practical exercises, I often get stumped. I tend to like that because it is forcing me to learn how to research and work through my issues as they arise - and, for me, I generally learn very well that way.
With that said, there are some hurdles that I just can't seem to figure out. I have the answers to the practical exercises if I want to look at them, but I do not want to do that --- as I feel like I learn very little that way.
I really do not want to share what the task is because I do not want help solving it - and I do not think anyone attempting to help me will need to know that anyways.
In the code below, I would just like help understanding what is actually happening. I know, I know, that sounds extremely general, but I am too new to really know how to properly articulate my question.
I guess my question boils down to this: Why is my count of elements in the 'listNumbers' list showing zero?
My logic is this:
I ask the user for input separated by hyphen. I take that input and split at the hyphen into a string array. I create an empty list. I loop through each element of the string array, convert to int32, and add to the list. I put a counter up to see how many times it was looping - which, shows accurately based on how many numbers are entered initially. I loop through each element of the list with a second counter to see how many times it was looping - which also seems to show accurately based on how many numbers are entered initially.
Nonetheless, my count of elements in the list shows as zero - like there are no elements in the list.
How can that be?
Can someone please explain why my count of elements in the list is showing zero?
Thank you all so much.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter a few numbers separated by a hyphen and then press 'Enter'.");
var userInput = Console.ReadLine();
var numbers = userInput.Split('-');
var listNumbers = new List<Int32>();
var counter1 = 0; // Just for troubleshooting
var counter2 = 0; // Just for troubleshooting
var count = listNumbers.Count;
foreach (string number in numbers)
{
var convertedNumber = Convert.ToInt32(number);
listNumbers.Add(convertedNumber);
counter1++; // Just for troubleshooting
}
foreach (int listNumber in listNumbers)
{
counter2++; // Just for troubleshooting
Console.WriteLine(listNumber); // Just for troubleshooting
}
// Just for troubleshooting
Console.WriteLine("----------");
Console.WriteLine("Count of loops through coversion from string to int32 and adding each to list: " + counter1);
Console.WriteLine("Count of loops through the list itself: " + counter2);
Console.WriteLine("Count of elements in list: " + count);
}
}
}
Upvotes: 1
Views: 1650
Reputation: 7183
Simply change:
Console.WriteLine("Count of elements in list: " + count);
To
Console.WriteLine("Count of elements in list: " + listNumbers.Count);
You are currently counting the items in the collection before you add new items. To get an updated list of the count you need to call the listNumbers.Count fuction after you have added your items.
Upvotes: 0
Reputation: 1164
You only assigned a value to the variable "count" when the list had zero elements. The local "count" variable you've declared does not auto-update when the list grows; only the 'Count' property on the list itself does that.
What you probably want is not the number of elements in the list when empty, but rather the number of elements in the list after you've populated it, in which case you need to get the Count of the list after adding all elements to it.
So remove the following line:
var count = listNumbers.Count;
And add the exact same line immediately after the two foreach loops.
Or, since you're only referencing that variable once, you can just remove the above count variable completely, and then replace the last Console.WriteLine with the following:
Console.WriteLine("Count of elements in list: " + listNumbers.Count);
Upvotes: 6