Subscribe to Blog.Parse(typeof(PsychoCoder), Richard McCutchen);        RSS Feed
-----

C# 4.0 Countdown : New Features - Optional & Named Parameters

Icon Leave Comment
With the release of Visual Studio 2010 & .Net 4.0 just 4 days I thought I'd do a few blog posts highlighting some of the new features of .Net 4.0. The first we will look at is optional parameters. Most believe Microsoft has added this feature as part of their plans to create a single unified .Net language.

The addition of this to the C# language has sparked any debates in the programming world as to whether this is good or bad, I do believe that at times it will come in handy to shorten your code and make it more concise, such as when dealing with method overloading or with COM Interop (another mission of the optional parameter being added to the C# language).

Let's take a look at a hypothetical situation. Let's say we have an email class that send emails, and we want to be able to send different parameters depending on the situation. In todays world we could have any number of methods doing the same thing

public void SendMyMail(string to, string from)
{
    //do your code here
}

public void SendMyMail(string to, string from, string body)
{
     //code here
}

public void SendMyMail(string to, string from, string body, bool isHtml)
{
     //code here
}



With this your code can get very convoluted very fast, especially if there are more choices than listed above. Now with C# 4.0 we can replace the above three methods with a single method using optional parameters such as this

public void SendMyMail(string to, string from = "[email protected]", string body = "This is the body", bool sendNow = true, bool isHtml = false)
{
     //code here
}



Using Optional Parameters (as seen above) can help in reducing the number of overloaded methods you need to create, and using the example above you can call it like this and they will all call the same method:

//example 1
SendMyMail("[email protected]", "This is demonstrating the use of optional parameters in C# 4.0");

//example 2
SendMyMail("[email protected]", "[email protected]",  isHtml:true);



So with optional parameters the same method can be called in a number of different ways, depending on what parameters are available and considered optional. Remember though, optional parameters must come after any required parameters, which means this will cause a compiler error:

public void SendMyMail(bool isHtml = true, string to, string body, bool sendNow)
{ 
     //code here
}



The next feature we'll be looking at is Named Parameters, which are most often used in conjunction with optional parameters. In my option Named Parameters will come in quite handy, granted they shouldn't be used for every situation but in terms of making code more readable it's a true blessing. Most of the time you see parameters passed to a method, and unless you wrote the method you have no idea what the parameters mean or are for, take this example

SendMyMail("[email protected]", "[email protected]", "Body of email", true, true);



Unless you wrote the method SendMyMail you wouldn't know what the parameters are, well the emails and body text are pretty obvious but what about the booleans values. With Names Parameters you would know their meaning, such as in this example:

SendMyMail(to:[email protected], from:"[email protected]", body:"Body of email", sendNow:true, isHtml:true)



So as you can see when taking over (or using) someone else's code you can see immediately what the parameters are that you're passing to their code (or when someone is using your code). Named parameters can be used not only on these constructors, but also methods, and properties with indexers, and normally appear when optional parameters are used in the signature of a method (hence listing them together here)

Tomorrow we will look at more new features coming in C# 4.0

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

July 2020

S M T W T F S
   1234
567891011
1213 14 15161718
19202122232425
262728293031 

Recent Entries

Search My Blog

3 user(s) viewing

3 Guests
0 member(s)
0 anonymous member(s)