C# Named Parameters

Method call

Named parameters are an alternate parameter syntax. They sometimes result in easier to read and clearer code. They are checked for correctness by the compiler. By specifying the formal parameter name, we can reorder arguments.

Arguments, Parameters

Example

Main method

In Main, we use the syntax "name:" and then a string literal to specify the value of the name parameter. The syntax "size:" and then an integer signifies the size parameter. You can reorder these named parameters in any way you want.

Also:You can specify names on only some parameters, using the positional syntax for some arguments.

C# program that uses named parameters

using System;

class Program
{
    static void Main()
    {
	// Call the Test method several times in different ways.
	Test(name: "Perl", size: 5);
	Test(name: "Dot", size: -1);
	Test(6, "Net");
	Test(7, name: "Google");
    }

    static void Test(int size, string name)
    {
	Console.WriteLine("Size = {0}, Name = {1}", size, name);
    }
}

Output

Size = 5, Name = Perl
Size = -1, Name = Dot
Size = 6, Name = Net
Size = 7, Name = Google
Abstract squares

In the C# language, programs are compiled into a high-level abstract assembly language. For the named parameters feature, the compiler uses temporary local variables. It then reorders those locals in the argument slots.

Intermediate Language

Tip:For named parameters, the compiler simply infers the regular order from the new syntax.

Performance

Performance optimization

The intermediate language representation of named parameters introduces extra local variables and assignments before passing the arguments. To demonstrate the performance effect, we introduce two methods, Method1 and Method2.

And:In these methods, a third method Method3 is called with named parameter syntax and classic parameter syntax.

Note:Please disregard the logic of Method3 as it is there mainly to ensure the method is not inlined.

C# program that tests named parameter performance

using System;
using System.Diagnostics;

class Program
{
    const int _max = 100000000;
    static void Main()
    {
	Method1();
	Method2();

	var s1 = Stopwatch.StartNew();
	for (int i = 0; i < _max; i++)
	{
	    Method1();
	}
	s1.Stop();
	var s2 = Stopwatch.StartNew();
	for (int i = 0; i < _max; i++)
	{
	    Method2();
	}
	s2.Stop();
	Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) /
	    _max).ToString("0.00 ns"));
	Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) /
	    _max).ToString("0.00 ns"));
	Console.Read();
    }

    static void Method1()
    {
	Method3(flag: true, size: 1, name: "Perl");
    }

    static void Method2()
    {
	Method3(1, "Perl", true);
    }

    static void Method3(int size, string name, bool flag)
    {
	if (!flag && size != -1 && name != null)
	{
	    throw new Exception();
	}
    }
}

Output

2.88 ns [named parameters]
0.33 ns
Framework: NET

The results for the .NET Framework 4.0 are interesting. They show that named parameters have a substantial performance drawback. The likely reason for this is that the named parameters are transformed into local variables.

So:As Method1 is called, memory on the virtualized stack must be allocated each time.

Therefore:More stack memory is used. This causes performance to suffer, making each method call slower.

This section provides information

It is unfortunate that the C# compiler cannot optimize named parameter calls into the performance equivalent of regular method calls. If every method call on your computer was 2.5 nanoseconds slower, your computer would be miserable.

Thus:Named parameters should be avoided in most C# program contexts where they do not dramatically increase readability.

Summary

The C# programming language

The named parameters feature in new versions of the C# language introduces syntactic sugar for method calls. Perhaps most intriguing, this feature embeds more contextual information into method calls.

And:This makes source text possibly clearer to read and understand. This can lead to less buggy programs.

Syntactic Sugar: Programming Term

C#: Method