Okay, we saw Optional Parameters, an upcoming feature of C# 4.0, in the last post and one reader of my blog asked me a question related to “Optional Parameters”. I don’t believe I have readers of my blog as well 🙂 but that was also one of my friend :p

So, in last post, we discussed an example of Employee class in which we passed some optional parameters in constructor:

public Employee(string firstName, string lastName, string qualification = "N/A", string middleName = "")

and I can simply call that like:

Employee emp= new Employee("Adil", "Mughal");

The great question was that Is there any way that we can skip qualification i.e. third parameter and give last parameter of middleName? He seems to be good reader or he might have listened to Anders Hejlsberg’s session 😉

The answer of this question is that Yes absolutely we can and that feature is called Named Argument in C# 4.0. We can simply do this like:

Employee emp = new Employee("Adil", "Mughal", middleName: "Ahmed");

Good enough to answer the query :). Now let’s do some changes with the Employee constructor and make lastName optional as well:

public Employee(string firstName, string lastName = "", string qualification = "N/A", string middleName = "")

Now I can instantiate object of Employee in quite simple and flexible ways

Employee("Adil", qualification:"BS");
Employee("ABC", lastName: "EFG", qualification: "BS");
Employee("XYZ", middleName: "MNO");

These upcoming features are really cool as they will improve productivity of cool enough to help C# developers avoid writing ‘n’ number of overloads though some of them are not new in the programming languages’ world.

Categories: C#


The Genius · April 20, 2009 at 9:57 am

thanks for this great post, its really cool 🙂

PShah · April 26, 2009 at 5:14 pm

Hey Adil – I read ur guidance on microsoft forums – http://social.microsoft.com/Forums/en-US/CertGeneral/thread/8312328d-8a02-4f5f-b0bf-b078230ebb96

I have gone through all the study material etc for 70-536 and now am almost ready for giving the exam. Been studying for over 2 months.

As you mentioned there is no harm to go through the dumps at the end to make sure u are on right track. Do you have them. If so – can you please email them to me at rahulmehta309@gmail.com.

Sorry there was no other way to contact you – hence doing through feedback

Thanks in advance

Umair Jabbar · April 30, 2009 at 8:28 am

microsoft adopting yet another thing from the open source languages
nice post though

Adil Mughal · April 30, 2009 at 9:56 am

@ The Genius: Thanks for the appreciation

@ PShah: I will reply you by email 🙂

@ Umair: Thanks. You are right but C# is quite strong language. There were reasons why Microsoft did not include such features earlier. Please vist the post http://blogs.msdn.com/samng/archive/2009/04/17/named-and-optional-arguments-ties-and-philosophies.aspx that explains the reason 🙂

Anonymous · May 13, 2010 at 1:16 pm

Very Good Feature ., It provided more readability in Logic

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *