Skip to main content

How to Give a Parameter a Default Value in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Many method calls use the same value for a parameter almost every time. In C#, give that parameter a default value, string greeting = "Hello", and callers can leave it out; those who need something else can still pass it.

Syntax​

Syntax
<ReturnType> <Name>(<Type> <required>, <Type> <optional> = <defaultValue>)
PartWhat it does
<Type> <required>A required parameter: every call must pass it.
<Type> <optional> = <defaultValue>An optional parameter: when a call leaves it out, it gets <defaultValue>.
<defaultValue>A value known when the code compiles, such as "Hello", 0, null or default.

Optional parameters go after all required ones. Microsoft's methods guide: "Optional parameters are specified by including the parameter's default value in the method definition. When the method is called, if no argument is supplied for an optional parameter, the default value is used instead."

The Example​

A Greet method takes a name and a greeting, which defaults to Hello. The program calls it once with the name only and once with both.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints Greet of Leo and Greet of Leo with Good morning, where the static Greet method takes a name and a greeting that defaults to Hello and returns the greeting, a comma, the name and an exclamation mark.

main.cs
Console.WriteLine(Greet("Leo"));
Console.WriteLine(Greet("Leo", "Good morning"));

static string Greet(string name, string greeting = "Hello")
=> $"{greeting}, {name}!";

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Hello, Leo!
Good morning, Leo!

A zsh terminal where dotnet run main.cs prints Hello, Leo! and Good morning, Leo!

The first call left greeting out, so it was Hello. The second passed Good morning, which replaced the default. The command is the same in zsh, bash and PowerShell.

What Happens When the Optional Parameter Comes First​

Put the optional parameter before the required one:

main.cs
Console.WriteLine(Greet("Leo"));

static string Greet(string greeting = "Hello", string name)
=> $"{greeting}, {name}!";
Output
/Users/you/demo/main.cs(1,19): error CS7036: There is no argument given that corresponds to the required parameter 'name' of 'Greet(string, string)'
/Users/you/demo/main.cs(3,59): error CS1737: Optional parameters must appear after all required parameters

The build failed. Fix the build errors and run again.

CS1737 is the real problem. Arguments are matched by position, so "Leo" went to greeting and name got nothing, which is the CS7036 above it. Microsoft's optional arguments guide: "Optional parameters are defined at the end of the parameter list, after any required parameters."

Common Mistakes​

MistakeWhat happensDo this instead
Putting an optional parameter before a required oneerror CS1737: Optional parameters must appear after all required parametersMove optional parameters to the end
Using a value computed at run time as the default, such as DateTime.NowNot allowed: the default must be known when the code compilesDefault to null, and compute the value inside the method
Changing a default in a library that other programs callCallers that were compiled earlier keep using the old value until they're rebuiltTreat defaults in shared code as part of its contract

Troubleshooting​

SymptomLikely causeFix
CS1737An optional parameter comes before a required oneReorder the parameters
CS7036 naming a parameter you thought was optionalIt has no default value, or a later argument is missingAdd the default, or pass the argument

FAQ​

What can a default value be?​

The optional arguments guide: "A default value must be one of the following types of expressions:" a constant expression; "an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct"; or "an expression of the form default(ValType), where ValType is a value type." That's why you often see CancellationToken cancellationToken = default.

Can a call skip one optional parameter and pass a later one?​

Not by position, but by name. With named arguments, a call can pass only the optional parameters it cares about, in any order.

Conclusion​

Write <Type> <name> = <defaultValue> to make a parameter optional, keep optional parameters at the end of the list, and use a value the compiler knows, such as a constant, null or default. Callers then pass only what differs from the default.

Additional Resources​