C# 4.0 New Features: Optional Parameters in Methods

Microsoft has provided several new features in .Net 4.0 and among them one which i found a very useful and handy is that now instead of making overloaded methods developer can just make a single method and make the parameters as optional.

Following OOPs people used to write overloaded methods for handling multiple parameters in different way. Lets suppose you have a method named Execute() and following are the overloaded methods.


void Execute(int a){}
void Execute(int a, int b){}
void Execute(int a, int b, int c){}

Using .Net framework 4.0 you dont need to create multiple overloaded methods and you just have to create one as follows.


void Execute(int a, int optionalB = 0, int optionalC = 0)
{

}

And it can be called as follows.


Execute(1);
Execute(1,2);
Execute(1,2,3);

We can also pass the parameters in different orders considering the same method as follows.


Execute(optionalB: 2, optionalC: 3, a:1);

You only need to denote the parameter name followed with a colon and the framework automatically sets the value to that order parameter have in the method signature.

Advertisements

75 thoughts on “C# 4.0 New Features: Optional Parameters in Methods

  1. I believe this internet site has some real great info for everyone. “I have learned to use the word ‘impossible’ with the greatest caution.” by Wernher von Braun.

  2. My spouse and I stumbled over here coming from a different web address and thought I may as well check things out. I like what I see so i am just following you. Look forward to looking over your web page repeatedly.

  3. I’ll right away seize your rss as I can not find your email subscription link or e-newsletter service. Do you’ve any? Please permit me realize in order that I could subscribe. Thanks.

  4. Good site! I really love how it is easy on my eyes and the data are well written. I’m wondering how I could be notified when a new post has been made. I have subscribed to your feed which must do the trick! Have a nice day!

  5. Right after study some with the websites along with your internet website now, i genuinely as if your way of blogging. I bookmarked it to my bookmark website list and will likely be checking back soon. Pls look at my internet site likewise and figure out what you believe.

  6. Good blog! I really love how it’s easy on my eyes as well as the data are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your rss feed which should do the trick! Have a nice day!

  7. thank you for all your efforts that you have put in this. Very interesting information. “Nothing else in the world…not all the armies…is so powerful as an idea whose time has come.” by Victor Hugo.

  8. Good blog! I truly love how it is simple on my eyes and the data are well written. I am wondering how I could be notified when a new post has been made. I’ve subscribed to your feed which must do the trick! Have a great day! “You can discover what your enemy fears most by observing the means he uses to frighten you.” by Eric Hoffer.

  9. I do consider all of the ideas you have introduced in your post. They are really convincing and will certainly work. Still, the posts are very quick for starters. May you please lengthen them a bit from next time? Thank you for the post.

  10. Good website! I really love how it is simple on my eyes and the data are well written. I am wondering how I might be notified whenever a new post has been made. I’ve subscribed to your feed which must do the trick! Have a nice day! “You can discover what your enemy fears most by observing the means he uses to frighten you.” by Eric Hoffer.

  11. Thanks, I’ve just been looking for info about this topic for a long time and yours is the best I’ve came upon so far. However, what concerning the bottom line? Are you certain about the supply?

  12. Unquestionably consider that that you said. Your favourite justification seemed to be at the web the simplest thing to remember of. I say to you, I definitely get irked while folks think about concerns that they plainly don’t realize about. You controlled to hit the nail upon the top as well as defined out the whole thing without having side-effects , other folks can take a signal. Will probably be back to get more. Thanks!

  13. You are my inhalation, I own few blogs and infrequently run out from brand :). “He who controls the past commands the future. He who commands the future conquers the past.” by George Orwell.

  14. I would like to take the opportunity of thanking you for the professional instruction I have enjoyed checking out your site. We are looking forward to the commencement of my university research and the overall planning would never have been complete without checking out your site. If I might be of any assistance to others, I’d personally be happy to help by way of what I have gained from here.

  15. I was recommended this blog by means of my cousin. I am no longer sure whether this post is written by means of him as nobody else recognize such distinctive about my difficulty. You’re incredible! Thank you!

  16. Thank you for your site post. Brown and I happen to be saving for our new publication on this matter and your blog post has made us all to save our money. Your ideas really responded all our concerns. In fact, more than what we had acknowledged before we stumbled on your wonderful blog. My spouse and i no longer have doubts plus a troubled mind because you have clearly attended to all of our needs in this article. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s