I know this is a basic question, but I couldn’t find an answer.
Why use it? if you write a function or a method that’s using it, when you remove it the code will still work perfectly, 100% as without it. E.g:
With params:
static public int addTwoEach(params int[] args)
{
int sum = 0;
foreach (var item in args)
sum += item + 2;
return sum;
}
Without params:
static public int addTwoEach(int[] args)
{
int sum = 0;
foreach (var item in args)
sum += item + 2;
return sum;
}
With
paramsyou can call your method like this:Without
params, you can’t.Additionally, you can call the method with an array as a parameter in both cases:
That is,
paramsallows you to use a shortcut when calling the method.Unrelated, you can drastically shorten your method: