While using the function member, any numbers of parameter (including none) may appear in the calling function based on need. So to have a flexible function we can use params modifier while writing the definition of functions.
The params keyword specify a method parameter that takes an argument where the numbers of arguments become variable means parameter arrays allow a variable numbers of arguments to be passed in to a function. It is also type compatible with the parameters. For example we can send integers , characters, strings at a time to the function which has the params modifier declared.
In addition to that, a single array can be passed in which parameter acts just as normal value parameter with the type matches as of the function.
For Example
private void ParamsUsed()
{
ArrayList listForObject = new ArrayList();
ArrayList listForInt = new ArrayList();
int[] myarray = new int[] { 10, 11, 12 ,13, 14, 15, 16 };
listForObject = UseParams2(100, 200, 'A', "amit");
listForInt = UseParams(myarray);
DropDownList1.DataSource = listForObject;
DropDownList1.DataBind();
DropDownList2.DataSource = listForInt;
DropDownList2.DataBind();
}
public static ArrayList UseParams2(params object[] list)
{
ArrayList listT = new ArrayList();
for (int i = 0; i < list.Length; i++)
{
listT.Add(list[i]);
}
return listT;
}
public static ArrayList UseParams(params int[] list)
{
ArrayList listT = new ArrayList();
for (int i = 0; i < list.Length; i++)
{
listT.Add(list[i]);
}
return listT;
}