Named and Optional Arguments
Named and optional arguments allows a lot of calling options (with defaults) but without the need of littering your code with overloads. Named Parameters allow you to specify your parameters in any order and omit any parameters with defaults as long as everything without a default value is specified.
Here’s an example of a method with named arguments:
public void ShowNamedArguments()
{
ShowValue(message: "Hello");
ShowValue(message: "Hello", newLine: false);
}
public void ShowValue(bool newLine = true, string message = "Default", string extraFooter = "")
{
Debug.Write(message);
if (newLine) Debug.Write(Environment.NewLine);
if (!string.IsNullOrEmpty(extraFooter)) Debug.Write(extraFooter);
}
Named and optional arguments allows a lot of calling options (with defaults) but without the need of littering your code with overloads. Named Parameters allow you to specify your parameters in any order and omit any parameters with defaults as long as everything without a default value is specified.
Here’s an example of a method with named arguments:
public void ShowNamedArguments()
{
ShowValue(message: "Hello");
ShowValue(message: "Hello", newLine: false);
}
public void ShowValue(bool newLine = true, string message = "Default", string extraFooter = "")
{
Debug.Write(message);
if (newLine) Debug.Write(Environment.NewLine);
if (!string.IsNullOrEmpty(extraFooter)) Debug.Write(extraFooter);
}