Help with Exercise from Ultimate C# Series: Part 1

My question:
Why doesn’t Mosh use split to remove the spaces? He only uses split to remove to commas which I assume leaves a string with numbers separated by spaces. I don’t understand this.

Context:
In Control Flow the second exercises has a question:

Write a program and ask the user to enter a series of numbers separated by comma. Find the maximum of the numbers and display it on the result. For example, if the user enters “5, 3, 8, 1, 4", the program should display 8 on the console.

In Mosh’s answer he does this:

public void Exercise5()
        {
            Console.Write("Enter commoa separated numbers: ");
            var input = Console.ReadLine();

            var numbers = input.Split(',');

            // Assume the first number is the max 
            var max = Convert.ToInt32(numbers[0]);

            foreach (var str in numbers)
            {
                var number = Convert.ToInt32(str);
                if (number > max)
                    max = number;
            }

            Console.WriteLine("Max is " + max);

        }

Hi,

An int has no space to worry about.

Just update the code a bit and see for yourself.

using System;
					
public class Program
{
	public static void Main()
	{
            Console.Write("Enter commoa separated numbers: ");
            //var input = Console.ReadLine();
			var input = "8, 12 , 1,4, 6,  16, 9    ,0";

            var numbers = input.Split(',');

            // Assume the first number is the max 
            var max = Convert.ToInt32(numbers[0]);

            foreach (var str in numbers)
            {
                var number = Convert.ToInt32(str);
				Console.WriteLine($"|{str}| =>|{number}|");
                if (number > max)
                    max = number;
            }

            Console.WriteLine("Max is " + max);
	}
}

Output

Enter commoa separated numbers: |8| =>|8|
| 12 | =>|12|
| 1| =>|1|
|4| =>|4|
| 6| =>|6|
|  16| =>|16|
| 9    | =>|9|
|0| =>|0|
Max is 16

Thanks. Why doesn’t int have no space to worry about? In a previous lesson we learnt that all character symbols are numbers for ascii. I looked up the ascii value for space and it is 32.

Hi,
Space is a character.
int is a number (integer) so, it simply keeps just the number information. There is no white space in there.
So what I mean by “no space to worry about” is simply that when we convert from string to int, any white-space is removed. This is what we can see in the sample output I did add in my previous answer.

The other thing you are talking about is how letters are actually corresponding to ASCII or Unicode tables.

A computer basically doesn’t know what A,a,B and b are. But it can deal with numbers (binary based). So letters are actually corresponding to numbers and represented on screen as characters.

If you want to know more on the details please refer to those standards I mentioned above.

Cheers.

Perhaps I am getting confused with casting a space character to an int versis Convert.ToInt32.