C# Named Parameters

With the introduction of C# 4.0, named parameters have been included in the language. Named parameters can be used to enhance the readability of source code and simplify some calls by specifying which parameters the argument values refer to.
In C# 3.0 and earlier versions, the arguments provided when calling a method would be evaluated in the order in which they appeared in the method’s signature. By default, this is also the means by which argument lists are supplied in C# 4.0. However, it is now possible to associate individual arguments with specific parameters when calling a method, constructor,indexer or delegate by using named parameters.
When using a named argument, you provide the name of the parameter that you wish to pass a value to within the method call. For calls that pass literal values, this can improve the readability of your source code. Instead of viewing the method signature or displaying Intelligence for the call, the parameter names can make the literals instantly understood.
We can demonstrate this with an example. The MessageSender class below includes a single method that simulates sending a message by outputting to the console. The parameters allow some text to be provided, along with two formatting options.

Class MessageSender
{
public bool SendMessage(string message, bool useQuotes, bool capitalize)
{
message = capitalize ? message.ToUpper() : message;
message = useQuotes ? string.Format(""{0}"", message) : message;
Console.WriteLine(message);
return true;
}
}

When calling the SendMessage method without named parameters, the following code could be used:

var sender = new MessageSender();
sender.SendMessage("Hello", false, true);

At first glance, the “message” parameter is reasonably simple to understand. The effect of the two Boolean values is less easy to determine without viewing the method’s signature. However, we can use named parameters for all three arguments to make their purposes obvious. This is achieved by adding the parameter name followed by a colon (:) prior to the value for each argument, as shown below:

sender.SendMessage(message: "Hello", useQuotes: false, capitalize: true);

When you are using named parameters, the arguments for a call no longer need to be passed in the order in which they appear in the member’s signature. You are free to provide them in any order, allowing you to make the purpose of a call even more obvious in some situations. The following example might not improve readability but does show that the order of named arguments is unimportant to the compiler.

sender.SendMessage(capitalize: true, useQuotes: false, message: "Hello");

You can mix named and unnamed arguments in a call as long as all of the unnamed arguments appear before any named ones. The unnamed arguments will be applied in the order that they appear in the method’s signature. This allows code such as the following, where the first argument’s purpose is obvious but where the readability of the two Boolean is improved by the naming:

sender.SendMessage("Hello", useQuotes: false, capitalize: true);

Named Arguments with Optional Parameters
Named parameters provide more flexibility when used with optional parameters. When you make calls to members that include several optional parameters, you can omit any parameter whilst supplying values to any others. To demonstrate, adjust the test method as follows to make the two Boolean properties optional:

public bool SendMessage(
string message, bool useQuotes = false, bool capitalize = false)
{
message = capitalize ? message.ToUpper() : message;
message = useQuotes ? string.Format(""{0}"", message) : message;
Console.WriteLine(message);
return true;
}

Calls to the above method that do not use named parameters must provide the arguments in the correct order. This would prevent you from supplying a value for the capitalize parameter whilst using the default value for useQuotes. With named parameters, this restriction is removed:

sender.SendMessage("Hello", capitalize: true);

Tagged , . Bookmark the permalink.

2 Responses to C# Named Parameters

  1. Narayanan says:

    Hi
    Good Day!
    Nice Post. I gave my understand given below:
    Simply says Identify the value of Parameter.Is it good one for development Side? I found one advantage of the Named Parameter that is you can declare a value of in order. Am i right? Because we can declare a Parameter Name on the calling Method.
    One query:
    Three Parameters in the ” SendMessage” Method.But you are calling two Parameters value . did you get any error?
    Please let me know your views.
    Thanks and Regards,
    Lakshmi Narayanan.S

    • Thanks Narayanan, In this “SendMessage” function there are 3 parameters and 2 parameters has default value i.e. “useQuotes = false” & “capitalize = false”. If we are not passing its value from calling function in that case it will take its default value and it didn’t throw any error….

Leave a Reply