Optional parameter

Two Fer
Two Fer in C#
public static class TwoFer
{
    public static string Speak(string name = "you")
    {
        return $"One for {name}, one for me.";
    }
}

We define a single Speak() method with one optional parameter: name. This optional parameter has its default value set to the string "you". The caller of a method with an optional parameter can either pass in a value (which is then used) or not pass in a value (which caused the default value to be used).

These calls are thus identical:

`Speak()`
`Speak("you")`

Within the method, we use string interpolation to build the return string where {name} is replaced with the value of the name parameter.

Shortening

We can shorten the method by rewriting it as an expression-bodied method:

public static string Speak(string name = "you") =>
    $"One for {name}, one for me.";

or

public static string Speak(string name = "you") => $"One for {name}, one for me.";

String formatting

The string formatting article discusses alternative ways to format the returned string.

Optional parameters vs. method overloads

The main alternative to using an optional parameter is to use a method overloading approach. If you're interested in how these two solutions compare to each other, go check out our optional parameters vs method overloads article.

24th Apr 2024 · Found it useful?