Optional ParametersUse optional parameters to call methods. View the implementation of optional parameters.
This page was last reviewed on Dec 13, 2021.
Optional parameters. Sometimes in C# programs we want to omit arguments to methods. Optional parameters allow this: an optional parameter has a default value.
Named Parameters
A method with an optional parameter can be called without that argument. Using this feature, we add default values for formal parameters. We explore the performance here.
An example. We introduce a method named "Method" that has 2 parameters. Each of the parameters is optional. We use special syntax form to declare them.
Tip To specify an optional parameter, assign the formal parameter in the method parameter list to an appropriate value.
Here We set the formal parameter "value" to 1, and the formal parameter "name" to "Perl."
And Whenever Method is called without a parameter specified, its default value is used instead in the method body.
Thus We can call Method() with no parameters. And we can call it with only an int parameter.
using System; class Program { static void Main() { // Omit the optional parameters. Method(); // Omit second optional parameter. Method(4); // You can't omit the first but keep the second. // Method("Dot"); // Classic calling syntax. Method(4, "Dot"); // Specify one named parameter. Method(name: "Sundar"); // Specify both named parameters. Method(value: 5, name: "Pichai"); } static void Method(int value = 1, string name = "Perl") { Console.WriteLine("value = {0}, name = {1}", value, name); } }
value = 1, name = Perl value = 4, name = Perl value = 4, name = Dot value = 1, name = Sundar value = 5, name = Pichai
Internals. If you are interested in programming languages, you will want to know some information about how various features are implemented.
Important For the optional parameter feature, an attribute is used to decorate the optional parameters.
And The attribute stores some object data about the default value of the parameter.
.method private hidebysig static void Method([opt] int32 'value', [opt] string name) cil managed { .param [1] = int32(1) .param [2] = string('Perl') .maxstack 8 L_0000: ldstr "value = {0}, name = {1}" L_0005: ldarg.0 L_0006: box int32 L_000b: ldarg.1 L_000c: call void [mscorlib]System.Console::WriteLine(string, object, object) L_0011: ret }
Performance. What performance effect do optional parameters have? After benchmarking optional parameters, I found no performance effect.
Important The C# compiler transforms the calls where parameters were omitted to regular syntax calls.
So Method calls that omit parameters are the same speed as those that use the standard syntax.
A summary. The optional parameter feature lets us specify methods that do not need all of their parameters specified. We use an assignment statement within the method parameter list.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 13, 2021 (edit link).
© 2007-2023 Sam Allen.