if you make a function or a method that's using params, when you remove params the code will still work perfectly, So whats the difference between them ? Lets have a look.
{int sum = 0;
With params:
public static int sumIntegers(params int[] args){int sum = 0;
foreach (int element in args){
sum = sum + element;
}
return sum;
}
Without params:
public static int sumIntegers(int[] args){
int sum = 0;
foreach (int element in args){
sum = sum + element;
}
return sum;
}Answer
With params you can call your method like this:
sumIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9);
Without params, you can’t.
Additionally, you can call the method with an array as a parameter in both cases:
sumIntegers(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
That is, params allows you to use a shortcut when calling the method.
No comments:
Post a Comment