DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Using optional parameters in C# 4.0

Using optional parameters in C# 4.0

Denzel D. user avatar by
Denzel D.
·
Apr. 26, 10 · Interview
Like (0)
Save
Tweet
Share
9.88K Views

Join the DZone community and get the full member experience.

Join For Free

Since the very beginning of .NET evolution many developers felt the need in optional parameters. VB.NET had them, while C# developers had to implement function overloads to represent various versions of the same function, therefore repeating the same code multiple times just to make it usable in various situations.

With C# 4.0 there are now optional parameters that can be set for functions and constructors. Optional parameters get a default value that is used if no explicit change for that value is specified. So, the optional parameter cannot be omitted directly by the developer – it still needs a value, but it can be omitted later in the function call.

Let’s take a sample function that prints in the Output window:

void PrintToOutput(string line1, string line2 = "This is optional")
{
Debug.Print(line1);
Debug.Print(line2);
}

Now, I am going to call it with all the parameters passed to the function:

PrintToOutput("Sample1", "Sample2");

The final output will look like this:
 


If you noticed, when you were typing the function call, once you opened the parenthesis, the parameters that need to be passed are listed. The optional parameter is shown in square brackets:
 


The default value is also shown, so that the developer knows what the function is going to use if the parameter is not specified. Now, I am going to call the same function, without the second parameter. Here it is:

PrintToOutput("Sample1");

The output will look differently – the function took the default value for the parameter, as none was specified in the call:
 


Prior to C# 4.0, this had to be done by overloading:

void PrintToOutput(string line1)
{
Debug.Print(line1);
}

void PrintToOutput(string line1, string line2)
{
Debug.Print(line1);
Debug.Print(line2);
}

Overloading is still present in C# 4.0, however, an analogue method to it is already available that can eventually save some space in the code window and make the code more readable. To demonstrate this on a very specific example, you will notice the change in code volumes when working with Office Interop. Since the majority of the calls can request numerous parameters, typing all of them can cause the code to become quite bulky, especially since many of the parameters are something like Type.Missing. Now, you can just pass the needed parameters and reduce the call to 2 or none parameters specified explicitly.

But let’s take a look under the hood of a function with optional parameters. To do this, I am going to launch the MSIL disassembler and open the compiled executable. When I look at the PrintToOutput function, I can see this IL interpretation:

.method private hidebysig instance void  PrintToOutput(string line1,
[opt] string line2) cil managed
{
.param [2] = "This is optional"
// Code size 16 (0x10)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: call void [System]System.Diagnostics.Debug::Print(string)
IL_0007: nop
IL_0008: ldarg.2
IL_0009: call void [System]System.Diagnostics.Debug::Print(string)
IL_000e: nop
IL_000f: ret
} // end of method Form1::PrintToOutput

As you see, the [opt] attribute directly specifies that the second parameter is optional, so it is not automatically interpreting the function from an overloaded point of view.

dev Typing Bracket (tournament) VB.NET Pass (software) Requests Space (architecture) .NET Attribute (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Cucumber.js Tutorial With Examples For Selenium JavaScript
  • 3 Main Pillars in ReactJS
  • 7 Most Sought-After Front-End Frameworks for Web Developers
  • HTTP vs Messaging for Microservices Communications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: