Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Understanding Optional Parameter and Named Arguments in C# 4.0

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Sep 2011CPOL3 min read 15.1K   1   1
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:


C#
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:

C#
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)


Written By
Software Developer (Senior) 1E
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRegarding the overhead I disagree. I think that the compiled... Pin
scosta_FST20-Sep-11 3:23
scosta_FST20-Sep-11 3:23 
Regarding the overhead I disagree.
I think that the compiled code is created to call the one method from less parameters to the one with more parameters.
I think, not sure.

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

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