The Brave Programmer - Blogging and coding
Not for the faint hearted
 

Blog Posts From The Brave Programmer

Minimize

Optional Parameters in c# 4.0

Aug 24

Written by:
2009/08/24 08:54 AM  RssIcon

C# 4.0 Optional Parameters While testing the new Visual Studio 2010 and the new C# 4.0 I’ve been able to play with some of the new features. We have already looked at Dynamic Types and Named Parameters. In this post we are going to look at Optional Parameters.

Optional Parameters, as with named types, have been in C, C++ and I think VB.net before. So the concept is not new, but the feature is new in C#.

What is Optional Parameters?

This is exactly what the name implies. You can have and call a method where you do not have to supply all the parameters for that method. If some or all of the parameters are declared as optional, then they are, of course, optional.

Named and Optional Parameters are really two distinct features, and allow us to either omit parameters which have a defined default value, and/or to pass parameters by name rather than position.

Instead of using overload methods you can use Optional Parameters. In C# we’d have several methods with different signatures where, in effect, we’re really just after default values.

Optional Parameter Example.

The thing with Optional Parameters is that it has to be a Named Parameter with a default value.Before you go ahead in this article I suggest that you read the post on Named Parameters first, if you haven’t done so already.

Consider the following simple example. Suppose we have a method declared as below:

public double CurrencyExchange(double amount, double rate)
{
return amount * rate;
}

 

We could then call this method supplying the needed values.

double rand = CurrencyExchange(100, 8.12);

The problem with this is if we wanted to only supply one value and have the second one as a default we would have to declare an override method.

But with Optional Parameters and Named Parameters we do not have to declare overrides. We can name the parameter, supply a default and call the method with one parameter.

Consider the same code with Optional Parameters:

public double CurrencyExchange(double amount, double rate = 8.12)
{
return amount * rate;
}

We could call this method then leaving the rate value out.

double rand = CurrencyExchange(100);

But if we wanted to supply a different rate, we just pass the new rate to the method.

 

double rand = CurrencyExchange(100,13.5);
 

Note:Just remember, as with named parameters, the optional or named parameter has to be at the end of the parameter list, especially if not all parameters are named.

You could also have both parameters optional as in:

public double CurrencyExchange(double amount=100, double rate = 8.12)
{
return amount * rate;
}

Then call the method leaving both parameters out.

 

double rand = CurrencyExchange();

Or we could call the method with the named parameter not worrying about parameter order.

double rand = CurrencyExchange(rate:7.6, amount:100)

Conclusion.

Obviously you won’t use Optional Parameters in every situation. It does though give the developer a wider range of options. Optional parameters will definitely help reduce the number of method overloads you need to write.

As with dynamic types, apparently the biggest beneficiaries of these new named and optional parameter features are developers who interop with Office. Just like in VBScript you can now avoid having to provide optional arguments, and can also provide a named argument directly.

Related Reading:

Dynamic Types in C# 4.0 – Visual Studio 2010

C# 4.0 New Features – Named Parameters

Testing Visual Studio 2010

Do you see the need to use Optional Parameters in your code? Do you think that it would be a benefit to you? Let us know your thoughts about this new C# 4.0 feature in the comments below.

New here, or perhaps you've been here a few times? Like this post? Why not subscribe to this blog and get the most up to date posts as soon as they are published.

 



Tags:
Categories:
blog comments powered by Disqus

1 comment(s) so far...


Gravatar

Re: Optional Parameters in c# 4.0

Nice post. It explained very well.
Myself has added a post on this topic. Please take a look at this.
http://cherupally.blogspot.com/2009/11/c-40-new-features-named-and-optional.html

Thanks,
Kiran

By Kiran Cherupally on   2009/11/06 08:59 AM
 
Blog Updates Via E-mail
  Blog Updates Via E-mail
Minimize

Do you want to receive blog updates via e-mail. Then just click on the link below. You will be redirected to Google's feed burner, where you can fill out a form. Supplying your e-mail address.

The subscription is managed entirely by Google's Feedburner. We cannot and do not collect your email address.

Subscribe to The Brave Programmer by Email

Print  
 

 

Latest Comments
  Latest Comments
Minimize
Powered by Disqus

Sign up with Disqus to enjoy a  surprise box of features

Print  
 
Blog Roll
  Blog Roll
Minimize
Print  
 
Categories
  Categories
Minimize
Print  
 
<h1>Search Blogs From The Brave Programmer</h1>
 

Search Blogs From The Brave Programmer

Minimize
Print  
 
Archive
  Archive
Minimize
Archive
<April 2024>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
Monthly
Go
Print  
 
<h1>News Feeds (RSS)</h1>
 

News Feeds (RSS)

Minimize
Print  
 

Follow robertbravery on Twitter

Blog Engage Blog Forum and Blogging Community, Free Blog Submissions and Blog Traffic, Blog Directory, Article Submissions, Blog Traffic

View Robert Bravery's profile on LinkedIn

Mybyte

 

Robert - Find me on Bloggers.com

Tags
  Tags
Minimize
Print  
 
Contact Us Now
  Contact Us Now
Minimize
 

Email  us now or call us on 082-413-1420,  to host your website.

We design and develop websites. We develop websites that make a difference. We do Dotnetnuke Module development.

Web Masters Around The World
Power By Ringsurf
Print