Refer to this answer: Pass multiple optional parameters to a C# function Question about Params (to a point). What I want to know is, how (if possible) can this be used to take user input rather than predefined numbers? So, I understand that I can do Console.Write(calculator.Add(1, 43, 23)) but would like to deal with user input.
class Program
{
static void Main(string[] args)
{
var calculator = new Calculator();
var input = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(calculator.Add(input));
}
}
public class Calculator
{
public int Add(params int[] numbers)
{
var sum = 0;
foreach (var number in numbers)
{
sum += number;
}
return sum;
}
}