Named and Optional Parameters in C#

Madhu.b.rokkam
Posted by in C# category on for Beginner level | Points: 250 | Views : 3188 red flag
Rating: 5 out of 5  
 2 vote(s)

This article explains this new feature in C# 4.0


 Download source code for Named and Optional Parameters in C#

Introduction

Among many new features in C# 4.0 we have Named and Optional Parameters as one of the enhanced feature, that has helped in bringing down the number of lines of code.

Named parameters enable you to specify a parameter by associating that with the parameter's name rather than with the parameter's position in the list of parameters.

Optional parameters enable you to omit or skip some parameters for some parameters list.

Lets See some examples

In case we have method called Add(int num1, int num2) we can call the same as

Add(10,20);

Add(num1:10, num2:20);

Add(10, num2:20);

Add(num2:20, num1:10);

All are same here in c# 4.0;

Let's see this example




Optional parametrs:

In C# earlier in order to call any method we had to pass all the parameters defined for that method and in sequence but now with C# 4.0 we can omit them.

In old way

In New way

That's it.. See how the number of lines of code is reduced.

Hope you all liked this article. Do let me know your comment.
Page copy protected against web site content infringement by Copyscape

About the Author

Madhu.b.rokkam
Full Name: Madhu Rokkam
Member Level: Bronze
Member Status: Member,MVP
Member Since: 1/13/2011 3:13:20 PM
Country: India
Thanks and Regards Madhu
http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Akiii on: 2/28/2011 | Points: 25
nice article......

Thanks and Regards
Akiii
Posted by: Madhu.b.rokkam on: 2/28/2011 | Points: 25
Thanks Akiii

Login to post response

Comment using Facebook(Author doesn't get notification)