Optional Parameters in c# 4.0
Aug24Written by:
2009/08/24 08:54 AM
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.
blog comments powered by
1 comment(s) so far...
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
|