C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Params: The params keyword is specified before the array type declaration in a parameter list in a method.
Tip: You can use the params keyword on instance or static methods. It must be the last argument in the parameter list.
SumParameters: This is a varargs method—it accepts a variable number of arguments.
Arguments: SumParameters is called with 1-4 arguments. It acts on these parameters and returns an integer value.
C# program that uses params keyword
using System;
class Program
{
static void Main()
{
// Call params method with one to four int arguments.
int sum1 = SumParameters(1);
int sum2 = SumParameters(1, 2);
int sum3 = SumParameters(3, 3, 3);
int sum4 = SumParameters(2, 2, 2, 2);
// ... Write results of the method invocations.
Console.WriteLine(sum1);
Console.WriteLine(sum2);
Console.WriteLine(sum3);
Console.WriteLine(sum4);
}
static int SumParameters(params int[] values)
{
// Loop through and sum the integers in the array.
int total = 0;
foreach (int value in values)
{
total += value;
}
return total;
}
}
Output
1
3
9
8
Note: In each case where SumParameters is found, a new int array of various lengths is allocated on the managed heap.
And: Because the integers are value types, their values are copied into this array.
Int ArrayDisassembled code inside Main method: C#
private static void Main()
{
int num = SumParameters(new int[] { 1 });
int num2 = SumParameters(new int[] { 1, 2 });
int num3 = SumParameters(new int[] { 3, 3, 3 });
int num4 = SumParameters(new int[] { 2, 2, 2, 2 });
Console.WriteLine(num);
Console.WriteLine(num2);
Console.WriteLine(num3);
Console.WriteLine(num4);
}
Version 1: This version of the method uses the params keyword in its formal parameter list.
Version 2: This version uses 3 int arguments. It has less capability than GetProduct() but also avoids an entire array on each call.
Result: Avoiding params is many times faster for this example. For numeric code, using params is a bad choice.
C# program that benchmarks params
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
class Program
{
static int GetProduct(params int[] values)
{
// Use params.
int result = 1;
foreach (int value in values)
{
result *= value;
}
return result;
}
static int GetProduct2(int value1, int value2, int value3)
{
return 1 * value1 * value2 * value3;
}
const int _max = 1000000;
static void Main()
{
// Version 1: use params method.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
GetProduct(2, 2, 3);
}
s1.Stop();
// Version 2: use int arguments.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
GetProduct2(2, 2, 3);
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
}
}
Output
9.11 ns params
0.27 ns int, int, int
Quote: Consider providing special overloads and code paths for calls with a small number of arguments in extremely performance-sensitive APIs. This makes it possible to avoid creating array objects (Framework Design Guidelines).