TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Optional Parameters

This C# article demonstrates optional parameters. It explores the feature's implementation.

Optional parameters. An optional parameter has a default value.

A method with an optional parameter can be called with only some of its parameters specified. Using this feature in new versions of the C# language, we add default values for formal parameters.

Example. In this example, we introduce a method named "Method" that has two parameters. Each of the parameters is optional. 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". Whenever Method is called without a parameter specified, its default value is used instead in the method body.

C# program that uses optional parameters

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: "Sam");

	// Specify both named parameters.
	Method(value: 5, name: "Allen");
    }

    static void Method(int value = 1, string name = "Perl")
    {
	Console.WriteLine("value = {0}, name = {1}", value, name);
    }
}

Output

value = 1, name = Perl
value = 4, name = Perl
value = 4, name = Dot
value = 1, name = Sam
value = 5, name = Allen

This example shows that you can call Method with no parameters. You can call it with only an int parameter. You can use the regular calling syntax from previous versions of the C# language. And you can use the named parameter feature.

Named Parameters

Internals. If you are interested in programming languages, you will want to know some information about how various features are implemented. 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. You can learn more about attributes on this site.

Attribute Examples

Intermediate language for optional parameter method: IL

.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. The C# compiler essentially 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.

Summary. The optional parameter feature enables a more concise way of specifying methods that do not need all of their parameters specified. By using an assignment statement within the method parameter list, we specify an optional parameter.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf