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);
}
}
}
count