In normal function we can allow fixed number of function arguments like
Int Add(int x,int y )
{
Return x+y;
}
But what if number of augments are not fixed we want that user allowed to pass
as much arguments he want to entered are allowed .In this case the parameter
arrays comes in handy.
We can define param array in Function argument by keyword "params" followed by
<type name> and then array name.
We will make a add function that will allow as many arguments as user wants to
input in function
class
Program
{
static int
Add(params int[]
nums)
{
int total=0;
foreach(int
i in nums)
{
total = total+i;
}
return total;
}
static void
Main(string[] args)
{
Console.WriteLine("Parameter
Array Function Testing ...");
int result=0;
/* function allowing 3 arguments */
result = Add(10, 10, 10);
Console.WriteLine("Result
for 3 Prameter :{0}", result);
/* function allowing 4 arguments */
result = Add(10, 10, 10, 10);
Console.WriteLine("Result
for 4 Prameter :{0}", result);
/* function allowing 5 arguments */
result = Add(10, 10, 10, 10,10);
Console.WriteLine("Result
for 5 Prameter :{0}", result);
/* function is also allowing whole array too */
int[] x = { 10, 10, 10, 10,
10, 10, 10, 10 };
Console.ForegroundColor =
ConsoleColor.Red;
result = Add(x);
Console.WriteLine("Result
for Array Summation Prameter :{0}", result);
Console.ReadKey();
}