What are Named Parameters?
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 Intellisense 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 capitalise) { message = capitalise ? message.ToUpper() : message; message = useQuotes ? string.Format("\"{0}\"", message) : message; Console.WriteLine(message); return true; } }
var sender = new MessageSender(); sender.SendMessage("Hello", false, true);
sender.SendMessage(message: "Hello", useQuotes: false, capitalise: 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(capitalise: 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 Booleans is improved by the naming:sender.SendMessage("Hello", useQuotes: false, capitalise: 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 capitalise = false) { message = capitalise ? message.ToUpper() : message; message = useQuotes ? string.Format("\"{0}\"", message) : message; Console.WriteLine(message); return true; }
sender.SendMessage("Hello", capitalise: true);
No comments:
Post a Comment