How to Give a Parameter a Default Value in C#
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
<ReturnType> <Name>(<Type> <required>, <Type> <optional> = <defaultValue>)
| Part | What 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
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

Console.WriteLine(Greet("Leo"));
Console.WriteLine(Greet("Leo", "Good morning"));
static string Greet(string name, string greeting = "Hello")
=> $"{greeting}, {name}!";
Step 2: Run It
dotnet run main.cs
Hello, Leo!
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:
Console.WriteLine(Greet("Leo"));
static string Greet(string greeting = "Hello", string name)
=> $"{greeting}, {name}!";
/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
| Mistake | What happens | Do this instead |
|---|---|---|
| Putting an optional parameter before a required one | error CS1737: Optional parameters must appear after all required parameters | Move optional parameters to the end |
Using a value computed at run time as the default, such as DateTime.Now | Not allowed: the default must be known when the code compiles | Default to null, and compute the value inside the method |
| Changing a default in a library that other programs call | Callers that were compiled earlier keep using the old value until they're rebuilt | Treat defaults in shared code as part of its contract |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CS1737 | An optional parameter comes before a required one | Reorder the parameters |
CS7036 naming a parameter you thought was optional | It has no default value, or a later argument is missing | Add 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
- Optional parameters and arguments, on Microsoft Learn.
- Named and optional arguments, on Microsoft Learn.
