C# named and optional arguments
last modified November 15, 2026
This article shows how to use named and optional arguments in C#. We declare parameters with default values, omit them at the call site, pass arguments by name, mix positional and named arguments, and look at the rules that govern defaults and overloads.
An optional parameter is a parameter with a default value. A caller
that does not supply an argument gets the default. A named argument
is written as name: value at the call site. It identifies the
parameter directly instead of relying on its position. Both features are
resolved by the compiler, so they do not change the method signature at run
time; the generated call simply supplies the missing values.
Named arguments are most useful when a method has several parameters of the same type, when a call would otherwise be a row of unexplained literals, or when only the last of many optional parameters needs a non-default value. Optional parameters are useful for settings that have a sensible default and for backwards-compatible additions to a public API.
Optional parameters
A parameter becomes optional when it declares a default value. All optional parameters must come after the required ones. A caller may stop at any optional parameter and let the rest keep their defaults.
var alice = CreateUser("Alice");
var bob = CreateUser("Bob", "admin");
var carol = CreateUser("Carol", active: false);
Console.WriteLine(alice);
Console.WriteLine(bob);
Console.WriteLine(carol);
static string CreateUser(string name, string role = "reader", bool active = true)
=> $"{name,-6} role={role,-6} active={active}";
The CreateUser method has one required parameter and two
optional ones. The first call supplies only the name, the second overrides
the role, and the third skips the role and sets active by name.
static string CreateUser(string name, string role = "reader", bool active = true)
The default values "reader" and true are part of
the method declaration, not of the call. Omitting an argument is not the
same as passing null: the parameter receives the declared
default. Because role is a non-nullable string,
the default cannot be null unless the parameter is declared as
string?.
$ dotnet run Alice role=reader active=True Bob role=admin active=True Carol role=reader active=False
Named arguments
A named argument uses the parameter name followed by a colon and the value. The order of named arguments does not have to match the declaration, which lets a call emphasize the values that matter and skip the ones that do not.
PrintOrder(item: "SSD", quantity: 2, price: 89.90m);
PrintOrder(quantity: 3, item: "RAM", price: 45.50m);
PrintOrder("Case", 1, 59.00m);
static void PrintOrder(string item, int quantity, decimal price)
{
Console.WriteLine($"{item}: {quantity} x {price:F2} = {quantity * price:F2}");
}
The first two calls name every argument, and the second also changes their order. The third call is positional and produces the same result as the first would with those values.
PrintOrder(quantity: 3, item: "RAM", price: 45.50m);
The compiler matches each name to a parameter of PrintOrder and
reorders the arguments in the generated call. A name that does not exist is
a compile-time error, and so is naming the same parameter twice. Renaming a
parameter is therefore a breaking change for callers that used it by name,
which is worth remembering for public APIs.
$ dotnet run SSD: 2 x 89.90 = 179.80 RAM: 3 x 45.50 = 136.50 Case: 1 x 59.00 = 59.00
Mixing positional and named arguments
A call can combine both styles. Positional arguments must come before named ones unless the named argument is in its own correct position, a relaxation added in C# 7.2. In practice, putting positional arguments first and named arguments afterwards is the easiest rule to read and to remember.
Send("report.pdf", priority: 2);
Send("photo.jpg", 1, compress: false);
Send(file: "notes.txt", compress: false);
static void Send(string file, int priority = 1, bool compress = true)
{
Console.WriteLine($"{file}: priority={priority}, compress={compress}");
}
The first call supplies the required argument positionally and names the second. The second call passes the first two positionally and names the third. The third call names the first and the third, skipping the middle one.
Send(file: "notes.txt", compress: false);
Skipping priority is possible only because it is optional and
because the remaining argument is named. A positional argument cannot skip
a parameter. This is the pattern to use when a method has a long list of
optional settings and only one of them needs a value.
$ dotnet run report.pdf: priority=2, compress=True photo.jpg: priority=1, compress=False notes.txt: priority=1, compress=False
Defaults, null, and default values
A default value must be a compile-time constant, default, or
null for a reference type. A value type parameter can use
default to get its zero value. When the absence of a value must
be distinguishable from a real one, make the optional parameter nullable and
substitute the real default inside the method.
Console.WriteLine(Connect("db.local"));
Console.WriteLine(Connect("db.local", TimeSpan.FromSeconds(5)));
Console.WriteLine(Connect("db.local", retries: 3));
static string Connect(string host, TimeSpan? timeout = null, int retries = 2)
{
TimeSpan actual = timeout ?? TimeSpan.FromSeconds(10);
return $"{host}: timeout={actual.TotalSeconds}s, retries={retries}";
}
The timeout parameter is nullable with a null
default. The method replaces null with ten seconds, while a
caller can still pass an explicit timeout. retries uses a
constant default.
TimeSpan actual = timeout ?? TimeSpan.FromSeconds(10);
Using null as the sentinel keeps the real default out of the
signature, which matters when the default is not a constant. A method
cannot write TimeSpan timeout = TimeSpan.FromSeconds(10),
because FromSeconds is a method call, not a constant. The same
rule rules out DateTime.Now and newly created objects as
defaults.
$ dotnet run db.local: timeout=10s, retries=2 db.local: timeout=5s, retries=2 db.local: timeout=10s, retries=3
Overloads and versioning
Optional parameters and overloads can express similar intent, and mixing
them can produce an ambiguous call. Two overloads that can both be satisfied
only by filling in defaults, such as Log(int level = 0) and
Log(string message = ""), make the call Log()
ambiguous and fail with compiler error CS0121. If one overload matches
exactly, as with Log() and Log(string message = ""),
the exact match wins.
Default values are compiled into the caller. If a library changes
role = "reader" to role = "guest", an application
that was already compiled keeps passing "reader" until it is
recompiled. Changing a default is therefore a silent behavioral change for
existing binaries, not a change that takes effect automatically. Adding an
optional parameter at the end of a parameter list is source compatible:
existing source calls compile unchanged. It is not binary compatible,
because the method signature changes and already-compiled callers still
reference the old one; keeping the previous overload avoids a
MissingMethodException. Inserting a parameter in the middle
breaks both source and binary compatibility.
For methods with many optional settings, an options object often ages better than a long parameter list. The constructor and property tutorials show the object initializer syntax that such an options type usually relies on.
Source
In this article we have used named and optional arguments in C#. See also the method, constructor, and interface tutorials.
Author
List all C# tutorials.