As per your requirement of "take multiple input on same line" you could do this as follow
List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
but there is a problem while we take multiple input on same line as "we can't restrict user to enter only given size of numbers". To avoid this we can give informative message(or we can say validation message) to user as follows
while (numbers.Any() && ((numbers.Count() < arraySize) || (numbers.Count() > arraySize)))
{
Console.WriteLine("You have enter the numbers might be less than or greater than given size");
Console.WriteLine("Please the numbers of given size");
numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
}
Finally, you can do sum as follows
int sum = 0;
foreach (var num in numbers)
{
sum += num;
}
//or you can use directly sum method as follows
//Console.WriteLine(numbers.Sum());
Program as follows
static void Main(string[] args)
{
int arraySize = int.Parse(Console.ReadLine());
List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
while (numbers.Any() && ((numbers.Count() < arraySize) || (numbers.Count() > arraySize)))
{
Console.WriteLine("You have enter the numbers might be less than or greater than given size");
Console.WriteLine("Please the numbers of given size");
numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
}
int sum = 0;
foreach (var num in numbers)
{
sum += num;
}
//or you can use directly sum method as follws
//Console.WriteLine(numbers.Sum());
Console.WriteLine(sum);
Console.Read();
}
Results:
3
1 2
You have enter the numbers might be less than or greater than given size
Please the numbers of given size
1 2 3 4
You have enter the numbers might be less than or greater than given size
Please the numbers of given size
1 2 3
6