A parameter can have a default value when the caller leaves it out.

default parameter A default parameter supplies a fallback argument value.

Default Parameters

suffix
DefaultParameters.cs
Replay: real traced execution (multi-file project)
using System;

class Program
{
    static string Label(string word, string suffix = "!")
    {
        return word + suffix;
    }

    static void Main()
    {
        string suffix = "!";
        string defaultLabel = Label("ready");
        string customLabel = Label("ready", suffix);

        Console.WriteLine($"suffix={suffix}");
        Console.WriteLine($"default={defaultLabel}");
        Console.WriteLine($"custom={customLabel}");
    }
}
using System;

class Program
{
    static string Label(string word, string suffix = "!")
    {
        return word + suffix;
    }

    static void Main()
    {
        string suffix = "?";
        string defaultLabel = Label("ready");
        string customLabel = Label("ready", suffix);

        Console.WriteLine($"suffix={suffix}");
        Console.WriteLine($"default={defaultLabel}");
        Console.WriteLine($"custom={customLabel}");
    }
}
using System;

class Program
{
    static string Label(string word, string suffix = "!")
    {
        return word + suffix;
    }

    static void Main()
    {
        string suffix = ".";
        string defaultLabel = Label("ready");
        string customLabel = Label("ready", suffix);

        Console.WriteLine($"suffix={suffix}");
        Console.WriteLine($"default={defaultLabel}");
        Console.WriteLine($"custom={customLabel}");
    }
}
  1. suffix ← !

    10static void Main()11{12    string suffix→ ! = "!"; //@suffix="?", "."13    string defaultLabel = Label("ready");14    string customLabel = Label("ready", suffix);
  2. static string Label(string word, string suffix = "!")

    pass 1 of 2
    4{5    static string Label(string wordready, string suffix = "!"!)6    {7        return wordready + suffix!;8    }
  3. defaultLabel ← ready!

    12string suffix = "!"; //@suffix="?", "."13string defaultLabel→ ready! = Label("ready");14string customLabel = Label("ready", suffix!);
  4. static string Label(string word, string suffix = "!")

    pass 2 of 2
    4{5    static string Label(string wordready, string suffix = "!"!)6    {7        return wordready + suffix!;8    }
  5. customLabel ← ready!

    13    string defaultLabel = Label("ready");14    string customLabel→ ready! = Label("ready", suffix!);1516    Console.WriteLine($"suffix={suffix!}");17    Console.WriteLine($"default={defaultLabelready!}");18    Console.WriteLine($"custom={customLabelready!}");19}
    outputsuffix=!
    default=ready!
    custom=ready!
  1. suffix ← ?

    10static void Main()11{12    string suffix→ ? = "?";13    string defaultLabel = Label("ready");14    string customLabel = Label("ready", suffix);
  2. static string Label(string word, string suffix = "!")

    pass 1 of 2
    4{5    static string Label(string wordready, string suffix = "!"!)6    {7        return wordready + suffix!;8    }
  3. defaultLabel ← ready!

    12string suffix = "?";13string defaultLabel→ ready! = Label("ready");14string customLabel = Label("ready", suffix?);
  4. static string Label(string word, string suffix = "!")

    pass 2 of 2
    4{5    static string Label(string wordready, string suffix = "!"?)6    {7        return wordready + suffix?;8    }
  5. customLabel ← ready?

    13    string defaultLabel = Label("ready");14    string customLabel→ ready? = Label("ready", suffix?);1516    Console.WriteLine($"suffix={suffix?}");17    Console.WriteLine($"default={defaultLabelready!}");18    Console.WriteLine($"custom={customLabelready?}");19}
    outputsuffix=?
    default=ready!
    custom=ready?
  1. suffix ← .

    10static void Main()11{12    string suffix→ . = ".";13    string defaultLabel = Label("ready");14    string customLabel = Label("ready", suffix);
  2. static string Label(string word, string suffix = "!")

    pass 1 of 2
    4{5    static string Label(string wordready, string suffix = "!"!)6    {7        return wordready + suffix!;8    }
  3. defaultLabel ← ready!

    12string suffix = ".";13string defaultLabel→ ready! = Label("ready");14string customLabel = Label("ready", suffix.);
  4. static string Label(string word, string suffix = "!")

    pass 2 of 2
    4{5    static string Label(string wordready, string suffix = "!".)6    {7        return wordready + suffix.;8    }
  5. customLabel ← ready.

    13    string defaultLabel = Label("ready");14    string customLabel→ ready. = Label("ready", suffix.);1516    Console.WriteLine($"suffix={suffix.}");17    Console.WriteLine($"default={defaultLabelready!}");18    Console.WriteLine($"custom={customLabelready.}");19}
    outputsuffix=.
    default=ready!
    custom=ready.