What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 10311 |  Welcome, Guest!   Register  Login
Home > Articles > C# > C# 4.0 New Features – Named and Optional Parameters

C# 4.0 New Features – Named and Optional Parameters

Article posted by Kunal2383 on 3/3/2010 | Views: 9473 | Category: C# | Level: Beginner red flag


In this post I will talk for one of the new feature in C# 4.0 – Named and Optional Parameter. Actually these are not a single feature but two different feature. You can get more benefit if you work with them together. So, what are those? Named parameter is a way to provide an parameter to the method using the name of the corresponding parameter instead of its position in the parameter list. Whereas, optional parameter allows you to omit arguments to member invocation.

Introduction
In this post I will talk for one of the new feature in C# 4.0 – Named and Optional Parameter. Actually these are not a single feature but two different feature. You can get more benefit if you work with them together. So, what are those? Named parameter is a way to provide an parameter to the method using the name of the corresponding parameter instead of its position in the parameter list. Whereas, optional parameter allows you to omit arguments to member invocation.
Optional Parameter

Let us discuss it in depth with a simple example. First start with the Optional Parameter. Suppose you are creating an Calculator application where you have to do some “Add” operation by passing two, three or four parameters to the method. What will you do if you are using C# 2.0 or 3.0? You will have no other choice than creating as many methods and passing the parameters in this which is known as method overloading.


public int Add(int a, int b);
public int Add(int a, int b, int c);
public int Add(int a, int b, int c, int d);

If you are using C# 4.0 just forget about method overloading by using the optional parameter. Optional parameter is a default value passes to the method signature. Let us go for modifying our previous example to use optional parameter.


public int Add(int a, int b, int c = 0, int d = 0);

Here we are passing default value to the parameter “c” and “d”. Hence you can call this method as per your requirement like this:


            Add(10, 20); // 10 + 20 + 0 + 0
Add(10, 20, 30); // 10 + 20 + 30 + 0
Add(10, 20, 30, 40); // 10 + 20 + 30 + 40

In the first case it will pass ‘0’ (zero) as the optional parameter c and d, in the second case the fourth parameter will be treated as ‘0’ (zero) and in the third case all the parameters are available with proper values.

Named Parameter

Now, let us think that we have a situation where we are creating an account of an user with the method named “CreateAccount” which has a non-optional parameter “Name” and two optional parameters “Address” & “Age”. Here in some scenario you want to pass only the “Name” and “Age”, how can you pass? Just think on it.


public void CreateAccount(string name, string address = "unknown", int age = 0);

Are you thinking of calling the method with an empty string or null value to the “Address” parameter? Oh, not really. There comes the another new feature of C# 4.0 called as “Named Parameter”. Using this you can call the method with proper name resolution or even you can change the position of the parameter while calling the method. Have a look into the following code example:


CreateAccount("Kunal", age: 28);
CreateAccount(address: "India", name: "Kunal");

First example shows calling the method without the middle parameter “Address” and the second example demonstrates calling the same method after changing the position of the parameter in the parameter list using the name of the parameter.

Conclusion

By this way you can create a single method with Named & Optional Parameter instead of overloading it for several times and use it everywhere you need as per your requirement. This gives a more cleaner code with less efforts. Enjoy working with this. Cheers.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:3 year(s)
Home page:http://www.kunal-chowdhury.com
Member since:Monday, March 01, 2010
Level:Starter
Status: [Member]
Biography:He is currently working as a Silverlight application developer. Has a very good skill over C#, XAML, Silverlight & WPF. He has a good working experience in Windows 7 application (including Multitouch) development.

During his professional career he worked in various technologies & delivered quality output. He never hesitates to take up challenges & work on the latest technologies in Microsoft platform.

He attended various software development competition & achieved different awards.

He is presently focusing on the RIA (Silverlight & WPF) & willing to become a Technology Specialist in Microsoft platform. Learning newer things, Blog posting & helping others in forums is one of his regular activity.

Specialties: Silverlight Application Development, WPF Application Development, Windows 7 Application Development
>> Write Response - Respond to this post and get points
Related Posts

From this Article you can know which is the best way in performance to concatenate a string.

I have demonstrated how to print Diamond shape of stars and Diamond Space in square using for loops.

This article explains this new feature in C# 4.0

ReplaceSingleQuote Replace Single Quote (') with Double Single Quotes ('')

This is a starters post which will guide how to configure the environment and visual studio to work with emgucv, emgu cv is a wrapper library for all the open cv functions.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 8:42:48 PM