params, optional and named arguments
params: any number of arguments
A params parameter is an array that the caller may fill with arguments instead of an array literal. It must be the last parameter, and a call may give it zero arguments.
Console.WriteLine(Sum(1, 2, 3));
Console.WriteLine(Sum());
Console.WriteLine(Sum(new[] { 4, 5 }));
static int Sum(params int[] numbers) => numbers.Sum();
dotnet run
6
0
9
Three calls, one method: the compiler packs the arguments into an array, and
an existing array may be passed as it is. The empty call gets an empty
array, which is why Sum() is 0 rather than an error.
The packing is an allocation per call, so a method that is called in a hot
loop with a fixed number of arguments is better off with plain parameters
plus one params overload for the rare long call.
Optional parameters
A parameter with a default value may be left out of the call, and the default is written where the parameter is declared:
Console.WriteLine(Greet("Ada"));
Console.WriteLine(Greet("Ada", "Dr."));
Console.WriteLine(Greet(title: "Prof.", name: "Grace"));
static string Greet(string name, string title = "Mx.") => $"hello, {title} {name}";
dotnet run
hello, Mx. Ada
hello, Dr. Ada
hello, Prof. Grace
The third call uses both features at once: it names the arguments, so the order in the call does not matter. A default value must be a compile-time constant, which rules out DateTime.Now and a value from a field; a method that needs such a default takes DateTime? when = null and fills it in the body.
Named arguments
name: value passes an argument by the name of its parameter. It reads better at a call with three bools or three strings, and it lets a caller skip an optional parameter in the middle:
Console.WriteLine(Format("ada"));
Console.WriteLine(Format("ada", upper: true));
Console.WriteLine(Format("ada", suffix: "!"));
static string Format(string value, bool upper = false, string suffix = "")
{
var text = value + suffix;
return upper ? text.ToUpper() : text.ToLower();
}
dotnet run
ada
ADA
ada!
The second call sets upper and keeps suffix empty; the third skips upper and sets only suffix, which a positional call could not do without writing false in the middle.
The order rules
- Required parameters come before optional ones, so a required parameter after an optional one is an error unless the call names it.
- Positional arguments come before named ones, and a named argument that is out of order may not be followed by a positional one.
- params is last and may be omitted entirely.
- An optional parameter is part of the signature: a method with string title = "Mx." and one with string title are different overloads, and the compiler treats a call with one argument as a call to the first.
A default value is baked into the calling assembly when it is compiled, so a library that changes a default does not change the behaviour of a program that was already built. That is one reason a public API often prefers an overload to a default.
Exercises
- Write Average(params double[] numbers) and call it with one, three and zero arguments. What does the empty call return?
- Put a required parameter after an optional one and read the error.
- Call Format with the named argument upper first and then a positional value. Read the error about the order.
- Change the default of title to "Dr." and call Greet("Ada") again. Which title do you get?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where must a params parameter stand?
Answer
Last in the parameter list
The arguments after the fixed ones all go into the array, so nothing may follow it.
-
What does this program print?
Console.WriteLine(Sum(1, 2, 3)); static int Sum(params int[] numbers) => numbers.Sum();Answer
6
The three arguments are packed into an array and the LINQ Sum adds them.
-
What does this program print?
Console.WriteLine(Greet("Ada")); static string Greet(string name, string title = "Mx.") => $"hello, {title} {name}";Answer
hello, Mx. Ada
The omitted argument takes the default, so the title is Mx.
-
What does a named argument let a caller do?
Answer
Skip an optional parameter in the middle of the list
The name selects the parameter, so the ones before it keep their defaults.
-
What does this program print?
Console.WriteLine(Format(5, suffix: "!")); static string Format(int value, bool upper = false, string suffix = "") { var text = value.ToString() + suffix; return upper ? text.ToUpper() : text.ToLower(); }Answer
5!
The named argument sets suffix and upper keeps its default false, so the text is 5 with an exclamation mark.
-
Which value can be a default of an optional parameter?
Answer
A compile-time constant such as 0 or "text"
The default is written into the assembly at compile time, so it must be a constant; a nullable plus a null check is the way out.
-
What does this program print?
Console.WriteLine(Sum()); static int Sum(params int[] numbers) => numbers.Length;Answer
0
A params parameter may be omitted, and the caller then passes an empty array whose length is 0.
-
What is the cost of a params call with a few arguments?
Answer
One array allocation per call
The compiler creates an array to hold the arguments, which is why a hot path prefers plain parameters.
-
What does this program print?
Console.WriteLine(Greet(title: "Dr.", name: "Ada")); static string Greet(string name, string title = "Mx.") => $"{title} {name}";Answer
Dr. Ada
Named arguments are matched by name, so the order in the call does not have to follow the declaration.
-
Why does a public API often prefer an overload to a default value?
Answer
A default is compiled into the caller, so changing it does not affect code that is already built
The caller bakes the old default in, so a rebuilt library with a new default changes nothing until the caller is rebuilt too.
The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev