params keyword in C#
params keyword allows to specify a method with parameter which takes variable number of arguments. It is an array type of parameter. With this type of parameter you can call a method each time with different number of arguments.
public static int Sum(params int[] args) { int sum = 0; foreach (int ele in args) sum += ele; return sum; }
Function Sum() shown in above code accepts parameter of type params. You can see that the parameter is same as array type, but it differs from array in several ways
You can call Sum() as:
Sum(1, 2); // will return 3 Sum(new int[] { 1, 2, 3 }); // will return 6 Sum(); // will return 0
Points to note while using params:
0 comments: