Today, we will learn about working with Params/ParamArray in DotNet,
As we know that, Params let us know methods to pass variable numbers of parameters. We pass a comma-separated values to params array.
Note:- 1).ParamArray will always be defined as a Last parameters in Method definition.
2). In C#, it is pronounced as params
3). In VB.net, it is pronounced as ParamArray
We can understand this by an example:-
private int calculate_values(params int[] values)
{
int total = 0;
try
{
if(values!=null)
{
foreach (int value in values)
{
total+ = value;
}
}
console.writeline(total);
}
catch(Exception ex)
{
throw ex;
}
return total;
}
public static void main(string[] args)
{
int total1 = calculate_values(1,2);
int total2 = calculate_values(1,2,3);
int total3 = calculate_values(1,2,3,10);
}
//Output will be
3
6,
16
In VB.Net, we can define ParamArray as
Private Function calculate_values(ByVal ParamArray values() As Integer) As Integer
//do same code here
End Function