C#: Inside Params

You want to create a method that receives a variable number of parameters of a certain type with the params keyword in the C# language. The params keyword is used to describe methods that can receive any number of parameters of the specified type, and it results in extra allocations and reduced performance but improves the flexibility of the calling patterns. Here we look at params.

Below is the example which could explain what happens inside when params is used and impact on performance.

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

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 How (compiler interpret and create IL code )

Here we 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);

}

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.


					
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s