'Reading n-length array separated by spaces

Do you know a better way to take user input from the console? The first line of input consists of a single number n, which is the length of our array. Second-line has n numbers separated by spaces. My code looks like this:

int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
string[] stringarray;
stringarray = Console.ReadLine()).Split(' ');
for (int i = 0; i < n; i++)
{
    arr[i] = Convert.ToInt32(stringarray[i]);
}


Solution 1:[1]

You can use Linq instead of loop like this line:

arr = Console.ReadLine().Split(' ').Select(e => Convert.ToInt32(e)).ToArray();

Final Code:

int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
arr = Console.ReadLine().Split(' ').Select(e => Convert.ToInt32(e)).ToArray();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hamed Naeemaei