Wednesday 12 October 2011

Params Keyword

In c# we can user the Params keyword and pass variable no of arguments to a function. It's much like using ParamArray in VisualBasic language. The syntax of params arguments is:
params datatype[] argument name

Note that the argument passed using params argument should be an single dimension array also it should be the last argument in the argument list for the function.

The params parmeter can then be accessed as an normal array.

public int sum(params int[] num)
{
int tot=0;
foreach(int i in num)
{
tot=tot+i;
}
return tot;
}

tot = sum(1,2,3);

No comments:

Post a Comment