Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Optional and named arguments

Reference for optional parameters and named arguments in C#: default values, call order rules, nonpositional named arguments, overload resolution and versioning pitfalls.

An optional parameter has a default value in the method declaration, so callers may leave the argument out. A named argument lets the caller write name: value to say which parameter a value is for, regardless of position. Together they replace many overloads and make calls with several booleans or numbers readable.

Default values must be compile-time constants, default, or new() of a value type. All optional parameters must come after the required ones (and before a params array).

C#
static void Connect(string host, int port = 443, bool secure = true) { }

Connect("example.com");                       // port 443, secure true
Connect("example.com", secure: false);        // skip port, name the next

Try it

C# Skipping optional arguments by name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Defaults, overloads and the new() trick

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
ReturnType Method(Type required, Type optional = constant, Type another = default);

Method(1);                         // positional
Method(1, optional: 5);            // named
Method(optional: 5, required: 1);  // named, any order
Method(1, another: 3);             // skip a middle optional parameter
Rule Description
Default value A constant expression, default, or new S() for a struct S. For reference types other than string, the only constant default is null.
Position Optional parameters follow all required ones.
Named arguments Allowed for any parameter. Since C# 7.2 a named argument can stay in its correct position and be followed by positional ones.
Skipping You can skip optional parameters only by naming the ones you do pass after the gap.

Errors: supplying the same parameter twice (positionally and by name), naming an unknown parameter, or omitting a required parameter is a compile error.

Usage notes

  • Defaults are baked into the caller. The compiler copies the default value into every call site. If you change a default in a library without recompiling its callers, they keep using the old value. For public library APIs, prefer overloads or a null default that the method replaces internally.
  • Prefer null defaults for non-constant values. Use Options? options = null and options ??= new Options(); inside, because new Options() is not a constant.
  • Overload resolution: when two overloads both fit, the one that needs no default arguments is preferred, as the second example shows. Mixing optional parameters and overloads easily becomes confusing; pick one style.
  • Named arguments document intent. Process(data, retry: true, verbose: false) is far clearer than Process(data, true, false). They are also resilient to the order of parameters changing.
  • Evaluation order follows the call, not the declaration: arguments are evaluated left to right as written, even if named in a different order.
  • params and optional parameters: a params parameter must come last, and optional parameters before it can only be omitted by passing all following positionals or naming.
  • Attributes: [Optional, DefaultParameterValue(1)] exists for COM interop, but normal C# code uses = value.
  • Constructors, indexers and delegates accept optional parameters and named arguments too; lambdas can have defaults since C# 12.