Wednesday, April 9, 2008

How to use variable number of parameters in C#

It is very easy to use variable number of parameters in C# with the help of params keyword.

Here is a small example which will find the largest integer in from a list of integers where the size of the list is unknown.

public int FindLargest(int Num, params int[] Nums)
{
int Large = Num;
foreach (int n in Nums)
{
if (n > Large)
{
Large = n;
}
}
return Large;
}

2 comments: