BLOG.CSHARPHELPER.COM: Use named and optional arguments in C#
Use named and optional arguments in C#
Usually I try to post examples in an older version of C# because it's easier for someone to move an older example to a more recent version than it is to move a recent example to an older version. Most of the C# language has remained unchanged for a long time so this isn't a problem. However, C# includes are a few useful features that were added relatively recently.
Named and optional arguments were added with C# 2010.
Named arguments let you use the names of arguments when you invoke a method. For example, suppose the Person class has a constructor that takes the arguments: name, address, email, sms_phone, voice_phone, and fax. Then your code could invoke the constructor like this:
Person person = new Person(name: "Rod Stephens",
email: "[email protected]",
address: "", sms_phone: "", voice_phone: "", fax: "");
This makes your code a bit more self-documenting. It also allows you to specify arguments in any order you wish.
While named arguments provide some benefit, they're really not all that exciting until you combine them with optional arguments. To make an argument optional, follow it with a default value in the method's declaration. For example, the following code shows Person class's constructor with the address, email, sms_phone, voice_phone, and fax parameters optional.
Now the program can use the following code to create a new Person object while specifying only the name (which is required because it doesn't have a default value in the constructor's declaration) and email arguments.
Person person = new Person(name: "Rod Stephens",
email: "[email protected]");
This is not only handy but it gives you a new capability that you didn't have before. Suppose you want the constructor to require name but not the other parameters. Without optional arguments, you could make several overloaded versions of the constructor, one that takes (name), one that takes (name, address), one that takes (name, address, email), and so forth.
That would let you create Person objects while omitting some of the arguments but it would not let you omit them in any combination. For example, you could not specify a name and email without the address. Even if you were willing to make overloaded constructors for all of the 32 possible combinations (if I've counted correctly), the compiler couldn't tell all of the versions apart because all of the arguments are strings.
Named and optional arguments let you provide and omit arguments in any combination you want with only a single constructor.
Optional arguments are also useful for setting default values easily. For example, without optional arguments you could provide a version of the constructor that takes values for all parameters and then use a second version similar to the following code to pass it default values for arguments that are omitted.
use code similar to the following to
This works but again only lets you handle certain combinations of omitted arguments and requires many different versions of the constructor.
Another approach would be to let the calling code pass null or some other value to "omit" arguments and then make the code check each one to see if it is null as in the following code.
This is a lot more cumbersome and inelegant than using optional and named arguments.
To summarize, named and optional arguments are particularly useful when:
A method has lots of parameters and you want to use names to help document the calling code
You want to provide default values for parameters
You want to let the calling code omit arguments in any combination
Comments