Methods and Parameters
Default Parameters
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
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}");
}
}
suffix ← !
10static void Main()11{12 string suffix→ ! = "!"; //@suffix="?", "."13 string defaultLabel = Label("ready");14 string customLabel = Label("ready", suffix);static string Label(string word, string suffix = "!")
pass 1 of 24{5 static string Label(string wordready, string suffix = "!"!)6 {7 return wordready + suffix!;8 }defaultLabel ← ready!
12string suffix = "!"; //@suffix="?", "."13string defaultLabel→ ready! = Label("ready");14string customLabel = Label("ready", suffix!);static string Label(string word, string suffix = "!")
pass 2 of 24{5 static string Label(string wordready, string suffix = "!"!)6 {7 return wordready + suffix!;8 }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!
suffix ← ?
10static void Main()11{12 string suffix→ ? = "?";13 string defaultLabel = Label("ready");14 string customLabel = Label("ready", suffix);static string Label(string word, string suffix = "!")
pass 1 of 24{5 static string Label(string wordready, string suffix = "!"!)6 {7 return wordready + suffix!;8 }defaultLabel ← ready!
12string suffix = "?";13string defaultLabel→ ready! = Label("ready");14string customLabel = Label("ready", suffix?);static string Label(string word, string suffix = "!")
pass 2 of 24{5 static string Label(string wordready, string suffix = "!"?)6 {7 return wordready + suffix?;8 }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?
suffix ← .
10static void Main()11{12 string suffix→ . = ".";13 string defaultLabel = Label("ready");14 string customLabel = Label("ready", suffix);static string Label(string word, string suffix = "!")
pass 1 of 24{5 static string Label(string wordready, string suffix = "!"!)6 {7 return wordready + suffix!;8 }defaultLabel ← ready!
12string suffix = ".";13string defaultLabel→ ready! = Label("ready");14string customLabel = Label("ready", suffix.);static string Label(string word, string suffix = "!")
pass 2 of 24{5 static string Label(string wordready, string suffix = "!".)6 {7 return wordready + suffix.;8 }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.