C# Params Method

Params keyword

Params enables methods to receive variable numbers of parameters. With params, the arguments passed to a method are changed by the compiler into elements in a temporary array—this array is then used in the receiving method. Params cause an allocation to occur, which reduces performance.

Keywords

This C# tutorial shows how to use the params keyword. Params allows a variable number of arguments.

Example

To begin, we see a program that uses the params keyword that accepts any number of integer values. Internally, that method sums the values of those integers and returns that value. The params keyword is specified before the array type declaration in a parameter list in a method. You can use the params keyword on instance or static methods, and it must be the last argument in the parameter list.

Program that uses params keyword [C#]

using System;

class Program
{
    static void Main()
    {
	//
	// Call params method with one to four integer constant parameters.
	//
	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
Main method

Description. The program first defines a Main entry point and in this method the SumParameters method is called with one, two, three and four parameters. All of these method calls go into the SumParameters method defined next in the program. The SumParameters method acts on these parameters and returns an integer value. These result values are then printed to the screen through Console.WriteLine.

Using params keyword before array parameter type. The params keyword is placed in the parameter list of the method signature of SumParameters above. You put the params keyword before the array type declaration. Note that you can only have a params argument at the end of a declaration list; you cannot have regular parameters following the params argument with commas in the parameter list of a method signature.

Implementation

.NET Framework information

Let's examine how the C# compiler actually interprets and writes the intermediate language for params methods. When a params method call is encountered, the parameters are put into a new array of the matching type from the method signature. So in each case where SumParameters is found, a new int[] array of various lengths is allocated on the managed heap. Because the integers are value types, their values are copied into this array.

Disassembled 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);
}
Method call

Array creation expression performance. Creating an array with a new array creation expression takes a measurable amount of time. This is normally slower than pushing parameters onto the evaluation stack. For this reason, performance-critical methods are not best implemented with params keywords.

No benchmarks included. This article does not include a benchmark for params methods such as this one. This is because the params keyword results in more intermediate language instructions and allocations, making it obviously slower than regular method invocations. Normal methods use the evaluation stack, not the managed heap, for invocations and my previous benchmarks have shown regular methods are considerably faster.

Design considerations

Framework Design Guidelines

The params keyword in the C# language is most useful when designing a library for other programmers to use widely. If you have complete control over all usages of the method, you may not need params because you can simply add the necessary overloads.

The book ".NET Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries" suggests that for performance, you provide common overloads that avoid the params keyword on some methods. The best examples of params methods are the string.Concat method and the string.Format method.

Framework Design Guidelines String Concat Programs string.Format Method

Alternatives

Programming tip

As mentioned, there are alternatives to using params. These alternatives will impact performance much less. First, you could use a set of overloads instead of params. Second, you could use optional parameters, a new feature in the C# language, where you can omit arguments if you just want the default value. Third, you could just require an array parameter.

Overload Method Optional Parameters

Summary

The C# programming language

We saw the params keyword in the C# language and .NET Framework and how it can be used to describe methods that have flexible parameter counts. We noted that this keyword can be used in the final position in the parameter list of methods and it will often reduce performance of method invocations. Finally we discussed some considerations and recommendations from Microsoft's design guidelines related to the params keyword.

Method Tips
.NET