Click here to Skip to main content
Click here to Skip to main content

Tagged as

Understanding Optional Parameter and Named Arguments in C# 4.0

, 12 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will help you in understanding Optional Parameters and Named Arguments introduced in C# 4.0

Introduction

Optional Parameter is a parameter for which its positional argument is optional. If in case no value is supplied to it, a default value is used for the same.

It depends on the requirement that whether a parameter need to be made as optional or not. One can make more than one parameter as optional, but need to take care that all the optional parameters must be at the end of the parameter list.

Background

A new feature brought in C# 4.0 called as Optional Parameters. In large part, this is due to Microsoft’s plan of co-evolution with C# and VB.NET since VB.NET has had this feature for a while.

Optional Parameter is C# 4.0

Optional methods can be used in replacement to cases where we use method overloading. Let’s have a look at it using this example:-

Let’s take the scenario where where we need to have a method use to send emails. As per requirement we need to have few cases we want to CC the manager and in some cases we want to send emails in rich HTML rather than plain text. this we lead us to the following methods:

public void SendEmail(string toAddress, string bodyText)
{
      SendEmail(toAddress, bodyText, false);
}
     
public void SendEmail(string toAddress, string bodyText, bool ccManager)
{
      SendEmail(toAddress, bodyText, ccManager, false);
}
    
public void SendEmail(string toAddress, string bodyText, bool ccManager, bool isHtmlBody)
{
      // Actual implementation of the method
}
So if consuming code calls the method SendEmail("shashank.bisen@abc.com", "Hello Mr. Bisen"); at last it will lead to call of last SendEmail method with parameter having value:
toAddress - shashank.bisen@abc.com
bodyText - Hello Mr. Bisen
ccManager - false
isHtmlBody - false
& if consuming code calls the method SendEmail("shashank.bisen@abc.com", "Hello Mr. Bisen", true); at last it will lead to call of last SendEmail method with parameter having value:
toAddress - shashank.bisen@abc.com
bodyText - Hello Mr. Bisen
ccManager - true 
isHtmlBody - false
The above code is simple method overloading and we are setting default values (false for CC the Manager, and false for HTML emails).

Now with C# 4.0 we can now make the code more concise by only having to implement 1 method:

public void SendMail(string toAddress, string bodyText, bool ccManager= false, bool isHtmlBody= false)
{
      // Actual implementation of the method
}

So if consuming code calls the method SendEmail("shashank.bisen@abc.com", "Hello Mr. Bisen"); it will lead to call of SendEmail method with parameter having value:
toAddress - shashank.bisen@abc.com
bodyText - Hello Mr. Bisen
ccManager - false
isHtmlBody - false
& if consuming code calls the method SendEmail("shashank.bisen@abc.com", "Hello Mr. Bisen", true); it will lead to call of SendEmail method with parameter having value:
toAddress - shashank.bisen@abc.com
bodyText - Hello Mr. Bisen
ccManager - true 
isHtmlBody - false
Hence we found that only one method is sufficient to provide the same outcome what the previously 3 overloaded method use to give.

Named Arguments

C# 4.0 now also support a new concept called “named arguments”. This allows you to explicitly name an argument that you like to pass to the method – instead of just identifying it by argument position.

So if consuming code calls the method SendMail("shashank.bisen@abc.com", "Hello Mr. Bisen", isHtmlBody: true); it will lead to call of SendEmail method with parameter having value:

toAddress - shashank.bisen@abc.com
bodyText - Hello Mr. Bisen
ccManager - false
isHtmlBody - true
& if consuming code calls the method SendMail("shashank.bisen@abc.com", "Hello Mr. Bisen", ccManager : true); it will lead to call of SendEmail method with parameter having value:
toAddress - shashank.bisen@abc.com
bodyText - Hello Mr. Bisen
ccManager - true
isHtmlBody - false
& if consuming code calls the method SendMail("shashank.bisen@abc.com", "Hello Mr. Bisen", ccManager : true, isHtmlBody: false); it will lead to call of SendEmail method with parameter having value:
toAddress - shashank.bisen@abc.com
bodyText - Hello Mr. Bisen
ccManager - true
isHtmlBody - false
& if consuming code calls the method SendMail("shashank.bisen@abc.com", "Hello Mr. Bisen", isHtmlBody: true, ccManager : true); it will lead to call of SendEmail method with parameter having value:
toAddress - shashank.bisen@abc.com
bodyText - Hello Mr. Bisen
ccManager - true
isHtmlBody - true
Hence we can see that using the concept of Named Arguments we can even change the order of optional parameters.

Conclusion

There has all ways been argument on usage of Optional parameters, I would like to throw light on both of them:-

Advantages

  • If you have multiple overloaded methods, then you either have multiple versions of the code, which presents a maintenance issue.
  • If you have overloaded methods, then one method (the one with less parameters) calls the 'next' method (the one with more parameters), which introduces the minor overhead of an additional function call.

Shortcomings

  • Less Cross Language Support.
  • They Impede flexibility and can introduce logic errors.
  • Limit the ability to Implement Coding Standards.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shashank Bisen
Software Developer (Senior) Icreon
India India
No Biography provided

Comments and Discussions

 
GeneralRegarding the overhead I disagree. I think that the compiled... Pinmemberscosta_FST20-Sep-11 4:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 12 Sep 2011
Article Copyright 2011 by Shashank Bisen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid