Syntax
MethodName ( params type[ ] variableName ) { }
Important
- C# Parameter array is declared with the params modifier.
- Parameter type must be declared as an array.
- Must be of a single-dimensional Array type.
- Single params array per method is allowed.
- Must be the last argument in the method’s parameter list.
MethodName ( int a, int b, params int[ ] c )
MethodName ( params int[ ] a, int b ) - Cannot combine with the modifiers ref and out.
MethodName ( ref params int[ ] a )
MethodName ( out params int[ ] a ) - Cannot be declared as an optional parameter, meaning cannot have a default value.
MethodName( params string[ ] a = null ) - Empty array will be used if no value specified.
C# Parameter Arrays Example 1
The number of arguments that you can specify here is countless; the only restriction is that they must all be of type int. You can even specify no arguments at all. Below is the call to Sum() method with different number of integer arguments:
int total;
// Pass in a comma-delimited list of integers...
total = Sum(1, 2);
Console.WriteLine($"1. Total : {total}");
total = Sum(1, 3, 4);
Console.WriteLine($"2. Total : {total}");
total = Sum(1, 2, 3, 4, 5);
Console.WriteLine($"3. Total : {total}");
// Pass an ordinary array of integers.
int[] numbers = { 6, 7, 8, 9, 10 };
total = Sum(numbers);
Console.WriteLine($"4. Total : {total}");
// No arguments, compiler will pass an array of 0 length
total = Sum();
Console.WriteLine($"5. Total : {total}");
C# treats the variable-length argument list as a one-dimensional array whose elements are all of the same type. Hence, the method body can manipulate the parameter numbers as an array of integers.
The function Sum() is defined using the params keyword to accept any number of int arguments:
public static int Sum(params int[] numbers)
{
int total = 0;
foreach (int number in numbers)
total += number;
return total;
}
// OR: with for loop
public static int Sum(params int[] numbers)
{
int total = 0;
for (int i = 0; i < numbers.Length; i++)
total += numbers[i];
return total;
}
The code in this function simply iterates through the values in the numbers array and adds the values together in a total variable and then returning the result.
Output
1. Total : 3
2. Total : 8
3. Total : 15
4. Total : 40
5. Total : 0
C# Parameter Arrays Example 2
What if not only the number of arguments varies but also the argument type?
You can pass method arguments of different types and these arguments will automatically be wrapped inside an object array:
PrintValues("One", 2, "Three", 4, "Five", 6);
//converted to
PrintValues(new object[] { "One", 2, "Three", 4, "Five", 6 });
// OR
object[] obj = new object[6];
obj[0] = 1;
obj[1] = "Two";
obj[2] = 3;
obj[3] = "Four";
obj[4] = 5;
obj[5] = "Six";
PrintValues(obj);
// Method
public static void PrintValues(params object[] obj)
{
foreach (object o in obj)
Console.WriteLine(o.ToString());
}
Output
1
Two
3
Four
5
Six
Terms Used
Single-dimensional
single-dimensional arrays also called vectors.