I was looking for how to enter several variables into one line at once in C #, so I came across this code:

string t = ""; Console.WriteLine("Введите координаты точки (через пробел):"); t = Console.ReadLine(); string[] tv = t.Split(' ').Where(x => x != "").ToArray(); /*Тут происходит билиберда, которую я не понимаю, особенно зачем нужно Where и что внутри */ int AX = int.Parse(tv[0]); int AY = int.Parse(tv[1]); 

And everything seems to be clear, except for 4 lines, you can comment on what is happening in it, thanks in advance. Ps everything works so

  • And can this be done any easier? For example, in C ++ you can simply cin >> a >> f; - Konovalov Maxim
  • if in Split second parameter is to pass StringSplitOptions.RemoveEmptyEntries there will be no need for .Where(x => x != "").ToArray() - Grundy

1 answer 1

The Split method splits a string by spaces.
In the result array, there may be null values ​​(see the upper examples in the Notes section ).

With the help of the Linq query, non-empty elements are selected (filtered out), so that after filtering the tv array contains only non-empty elements.
(and whether they are valid numbers - will determine the result of Parse )

  • thanks a lot - Konovalov Maxim
  • @Monovalov Maxim If you are given an exhaustive answer, mark it as correct (click on the checkbox next to the selected answer) - MBo