New Apps

Articles

Tips

Events

Improve Soft Skills

/ / Optional and Nested Parameters in C#

Optional Parameters

For many C# developers, the long wait for optional parameters is over with C# 4.0. An optional parameter lets you provide a default value and the caller has a choice of whether or not they want to provide an argument. In current and earlier versions of C#, you could simulate optional parameters like this:
// Overload with no parameter
public static string SayHello()
{
    return "Hey, You!";
}
    
// Overload with normal parameter
public static string SayHello(string name)
{
    return "Hey, " + name + "!";
}
The SayHello method above is overloaded with both an empty parameter list and a parameter list with a single string parameter. To the user of this code, the name parameter appears to be optional and both of the following calls work fine:
// Name is optional
var greetYou = SayHello();
    
// Can provide name if you want
var greetJoe = SayHello("Joe");
If you had methods with more parameters and wanted to provide a more flexible coding experience, you would provide more overloads. Many developers have said that this is cumbersome and leaves more code to maintain. If you’re of the same mind, then you’ll probably be pleased that C# 4.0 supports optional parameters. The snippet below shows an optional parameter, replacing the previous two overloads:
// Method with optional parameter
public static string SayHello(string name = "You")
{
    return "Hey, " + name + "!";
}


As shown in the previous snippet, the syntax to call an optional parameter requires assigning a default value to the parameter. In the listing above, "You" will be assigned toname if the caller does not provide a value.

A feature related to optional parameters is named parameters, which is discussed next.

Named Parameters

One of the primary purposes of named parameters is that they resolve ambiguity between parameters. For example, the following method contains two parameters that are strings:
public static void AddClubMember(
    string name,
    string email = "",
    DateTime? dateJoined = null)
{
    // Not yet implemented
}
The types of the first two parameters of the AddClubMember method above are bothstring, except that name is not optional, but email is optional. This example also demonstrates that you can default parameters to null.
AddClubMember(
    email: "joe@dot.net",
    name: "Joe",
    dateJoined: DateTime.Now);}


The call to AddClubMember above uses named parameters where the name is the same as the parameter name in the method declaration with an appended colon. Notice that I’ve shuffled the parameter order to demonstrate that you can change the order of parameters.

Named parameters are particularly useful when you have multiple optional parameters of the same type. In that case, you name the parameter that you want to set, telling C# which parameter your argument matches. The code below demonstrates a problem where named parameters are necessary:
AddClubMember(
    "joe@dot.net",
    dateJoined: DateTime.Now);}
If you recall, the first parameter of the AddClubMember method is name, a required parameter of type string. Since the first argument above is a string, C# will match that argument to the name parameter. Clearly, the value above is an email address, which presents you with a logical error that won’t be detected at either compile time nor run time when that line executes.

«
Next

Newer Post

»
Previous

Older Post

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

No comments :

Leave a Reply

Wednesday, April 20, 2016

Optional and Nested Parameters in C#

Optional Parameters

For many C# developers, the long wait for optional parameters is over with C# 4.0. An optional parameter lets you provide a default value and the caller has a choice of whether or not they want to provide an argument. In current and earlier versions of C#, you could simulate optional parameters like this:
// Overload with no parameter
public static string SayHello()
{
    return "Hey, You!";
}
    
// Overload with normal parameter
public static string SayHello(string name)
{
    return "Hey, " + name + "!";
}
The SayHello method above is overloaded with both an empty parameter list and a parameter list with a single string parameter. To the user of this code, the name parameter appears to be optional and both of the following calls work fine:
// Name is optional
var greetYou = SayHello();
    
// Can provide name if you want
var greetJoe = SayHello("Joe");
If you had methods with more parameters and wanted to provide a more flexible coding experience, you would provide more overloads. Many developers have said that this is cumbersome and leaves more code to maintain. If you’re of the same mind, then you’ll probably be pleased that C# 4.0 supports optional parameters. The snippet below shows an optional parameter, replacing the previous two overloads:
// Method with optional parameter
public static string SayHello(string name = "You")
{
    return "Hey, " + name + "!";
}


As shown in the previous snippet, the syntax to call an optional parameter requires assigning a default value to the parameter. In the listing above, "You" will be assigned toname if the caller does not provide a value.

A feature related to optional parameters is named parameters, which is discussed next.

Named Parameters

One of the primary purposes of named parameters is that they resolve ambiguity between parameters. For example, the following method contains two parameters that are strings:
public static void AddClubMember(
    string name,
    string email = "",
    DateTime? dateJoined = null)
{
    // Not yet implemented
}
The types of the first two parameters of the AddClubMember method above are bothstring, except that name is not optional, but email is optional. This example also demonstrates that you can default parameters to null.
AddClubMember(
    email: "joe@dot.net",
    name: "Joe",
    dateJoined: DateTime.Now);}


The call to AddClubMember above uses named parameters where the name is the same as the parameter name in the method declaration with an appended colon. Notice that I’ve shuffled the parameter order to demonstrate that you can change the order of parameters.

Named parameters are particularly useful when you have multiple optional parameters of the same type. In that case, you name the parameter that you want to set, telling C# which parameter your argument matches. The code below demonstrates a problem where named parameters are necessary:
AddClubMember(
    "joe@dot.net",
    dateJoined: DateTime.Now);}
If you recall, the first parameter of the AddClubMember method is name, a required parameter of type string. Since the first argument above is a string, C# will match that argument to the name parameter. Clearly, the value above is an email address, which presents you with a logical error that won’t be detected at either compile time nor run time when that line executes.

No comments:

Post a Comment

Social Impact