Q:

How to split a string of numbers on the white space character and convert to integers

I'm working on some homework and I need to get an input from the user which is a single line of numbers separated by spaces. I want to split this string and get the individual numbers out so that I can insert them into a Binary Search Tree. 
I tried the split function and was able to rid of the white space but I'm not sure how to "collect" the individual numbers.
        string data;
        string[] newdata = { };
        Console.WriteLine("Please enter a list of integers with spaces 
                           between each number.\n");
        data = Console.ReadLine();
        newdata = data.Split(null);

        Console.WriteLine(String.Join(Environment.NewLine, newdata));

I want to somehow collect the elements from newdata string array and convert them into integers but I'm having a tough time figuring out how to do that.

A:

Well, you could use Linq .Select method combined with .Split method:
List<int> newData = data.Split(' ').Select(int.Parse).ToList();

If you want user to be able to enter empty spaces we need to trim the resulting strings after split. For that we can use another overload of string.Split method that accepts StringSplitOptions :
List<int> newData = data.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();

Finally if you want to allow user to enter incorrect data at times and still get collection of valid ints you could use int.TryParse and filter out values that were parsed incorrectly:
List<int> newData = data.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
                        .Select(s => int.TryParse(s.Trim(), out var n) ? (int?)n : null)
                        .Where(x => x != null)
                        .Select(i => i.Value)
                        .ToList();

A:

OK as code:
var words = data.Split();
int i;
List<int> integers = new List<int>();
foreach(var s in words)
{
  if (int.TryParse(s, out i)) {integers.Add(i);}
}
// now you have a list of integers
// if using decimal, use decimal instead of integer

A:

Some smart LINQ answers are already provided, here is my extended step by step solution which also allows to ignore invalid numbers:
//Read input string
Console.Write("Input numbers separated by space: ");
string inputString = Console.ReadLine();
//Split by spaces
string[] splittedInput = inputString.Split(' ');
//Create a list to store all valid numbers
List<int> validNumbers = new List<int>();
//Iterate all splitted parts
foreach (string input in splittedInput)
{
    //Try to parse the splitted part
    if (int.TryParse(input, out int number) == true)
    {
        //Add the valid number
        validNumbers.Add(number);
    }
}
//Print all valid numbers
Console.WriteLine(string.Join(", ", validNumbers));

