Optional Parameters and Named Arguments in C# 4 (and a cool scenario w/ ASP.NET MVC 2)
Posted
by ScottGu
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by ScottGu
Published on Fri, 02 Apr 2010 08:28:42 GMT
Indexed on
2010/04/02
8:33 UTC
Read the original article
Hit count: 717
[In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/scottgu]
This is the seventeenth in a series of blog posts I’m doing on the upcoming VS 2010 and .NET 4 release.
Today’s post covers two new language feature being added to C# 4.0 – optional parameters and named arguments – as well as a cool way you can take advantage of optional parameters (both in VB and C#) with ASP.NET MVC 2.
Optional Parameters in C# 4.0
C# 4.0 now supports using optional parameters with methods, constructors, and indexers (note: VB has supported optional parameters for awhile).
Parameters are optional when a default value is specified as part of a declaration. For example, the method below takes two parameters – a “category” string parameter, and a “pageIndex” integer parameter. The “pageIndex” parameter has a default value of 0, and as such is an optional parameter:
When calling the above method we can explicitly pass two parameters to it:
Or we can omit passing the second optional parameter – in which case the default value of 0 will be passed:
Note that VS 2010’s Intellisense indicates when a parameter is optional, as well as what its default value is when statement completion is displayed:
Named Arguments and Optional Parameters in C# 4.0
C# 4.0 also now supports the concept of “named arguments”. This allows you to explicitly name an argument you are passing to a method – instead of just identifying it by argument position.
For example, I could write the code below to explicitly identify the second argument passed to the GetProductsByCategory method by name (making its usage a little more explicit):
Named arguments come in very useful when a method supports multiple optional parameters, and you want to specify which arguments you are passing. For example, below we have a method DoSomething that takes two optional parameters:
We could use named arguments to call the above method in any of the below ways:
Because both parameters are optional, in cases where only one (or zero) parameters is specified then the default value for any non-specified arguments is passed.
ASP.NET MVC 2 and Optional Parameters
One nice usage scenario where we can now take advantage of the optional parameter support of VB and C# is with ASP.NET MVC 2’s input binding support to Action methods on Controller classes.
For example, consider a scenario where we want to map URLs like “Products/Browse/Beverages” or “Products/Browse/Deserts” to a controller action method. We could do this by writing a URL routing rule that maps the URLs to a method like so:
We could then optionally use a “page” querystring value to indicate whether or not the results displayed by the Browse method should be paged – and if so which page of the results should be displayed. For example: /Products/Browse/Beverages?page=2.
With ASP.NET MVC 1 you would typically handle this scenario by adding a “page” parameter to the action method and make it a nullable int (which means it will be null if the “page” querystring value is not present). You could then write code like below to convert the nullable int to an int – and assign it a default value if it was not present in the querystring:
With ASP.NET MVC 2 you can now take advantage of the optional parameter support in VB and C# to express this behavior more concisely and clearly. Simply declare the action method parameter as an optional parameter with a default value:
C#
VB
If the “page” value is present in the querystring (e.g. /Products/Browse/Beverages?page=22) then it will be passed to the action method as an integer. If the “page” value is not in the querystring (e.g. /Products/Browse/Beverages) then the default value of 0 will be passed to the action method. This makes the code a little more concise and readable.
Summary
There are a bunch of great new language features coming to both C# and VB with VS 2010. The above two features (optional parameters and named parameters) are but two of them. I’ll blog about more in the weeks and months ahead.
If you are looking for a good book that summarizes all the language features in C# (including C# 4.0), as well provides a nice summary of the core .NET class libraries, you might also want to check out the newly released C# 4.0 in a Nutshell book from O’Reilly:
It does a very nice job of packing a lot of content in an easy to search and find samples format.
Hope this helps,
Scott
© ASP.net Weblogs or respective owner