C# params

params keyword indicates that the function may take multiple arguments.

public int f(params int[] lst)
{
int sum = 0;
for (int i=0; i< lst.Length; i++) sum += lst[i];
return sum;
}
int[] arr = {3, 4, 5};
int total = f(arr); //12
int[] arr2 = {12, 3, 4, 5};
int total2 = f(arr); //24
int total3 = f(12,3,4,5); //24

With params keyword, the function may take no arguments.
int total4 = f(); //0


endmemo.com © 2017  Terms of Use | Home