This C# keyword enables methods to receive variable numbers of parameters. The arguments passed to a method are changed (by the compiler) to elements in a temporary array.
The arguments array is then used in the receiving method. This impacts performance. But it can make your code look clean and elegant.
To begin, we use the params
keyword on a method that accepts integer values. That method internally sums the values of those integers and returns that value.
params
keyword is specified before the array type declaration in a parameter list in a method.params
keyword on instance or static
methods. It must be the last argument in the parameter list.SumParameters
is called with 1-4 arguments. It acts on these parameters and returns an integer value.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; } }1 3 9 8
Let us examine how the C# compiler handles params
methods. When a params
method call is encountered, the parameters are put into a new array.
SumParameters
is found, a new int
array of various lengths is allocated on the managed heap.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); }
Creating an array is slower than pushing parameters onto the evaluation stack. Performance-critical methods are not best implemented with params
keywords.
params
keyword in its formal parameter list.int
arguments. It has less capability than GetProduct()
but also avoids an entire array on each call.params
is many times faster for this example. For numeric code, using params
is a bad choice.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")); } }9.11 ns params 0.27 ns int, int, int
The params
keyword is most useful when designing a library. The best examples of params
methods include the string.Concat
method and the string.Format
method.
Params is used to describe methods that have flexible parameter counts. It is placed in the final position in the parameter list of methods.