
This is the seventeenth article in the
series , which is dedicated to the release of VS 2010 and .NET 4.
Today's post will touch on the topic of new language features added in C # 4.0 — optional parameters and named arguments, and another cool way to use optional parameters in ASP.NET MVC 2.
Optional parameters
C # 4.0 supports optional parameters in methods, constructors, and indexers (VB could do this a long time ago).
')
Parameters are optional when the default value is present in the declaration. For example, below is a method that takes two parameters: the string parameter “category” and the integer “pageIndex”. The “pageIndex” parameter has a default value of 0 and therefore is an optional parameter:
When calling the above method, we can explicitly pass two parameters:
Or omit the second optional parameter, in this case the default value of 0 will be passed:
Note that IntelliSense in VS 2010 indicates that the parameter is optional, as well as its default value:
Named arguments and optional parameters in C # 4.0
C # 4.0 also supports the concept of named parameters. That allows you to explicitly specify the name of the parameter passed to the method, instead of identifying it by position.
For example, I can write the code as shown below, explicitly defining the second argument in the
GetProductsByCategory method by name:
Named arguments are convenient enough when the method supports several optional parameters and you want to explicitly specify which arguments to pass. For example, below, we have a
DoSomething method that takes two optional parameters:
We can use named arguments to call a method in any of the following ways:
Since both parameters are optional, and in the case where one or zero arguments are specified, the default value for the unspecified argument is passed.
ASP.NET MVC 2 and optional parameters
Let's look at one interesting scenario when we can use optional parameters in ASP.NET MVC 2 communication with action methods in controller classes.
For example, we want to link such URLs as “Products / Browse / Beverages” or “Products / Browse / Deserts” to the controller's action method. We can accomplish this by writing a URL router that associates URLs with a method:
We can also use the value of the optional query string parameter “page”, but we can also ignore it to display the result of the Browse method, which displays the result by pages. For example, / Products / Browse / Beverages? Page = 2.
In ASP.NET MVC 1, you, as usual, processed this script by adding the “page” parameter to the action method and setting it to null (if the “page” parameter is not present in the query string, then null will be passed). Next, you can write code that converts null to int and assign it to the default value if it was not passed to the query string:
With ASP.NET MVC 2, you can use optional parameters supported in VB and C # for shorter and clearer implementation. It is enough to declare an action method parameter as optional with a default value:
C #
Vb
If the “page” value is present in the query string (/ 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 query string (/ Products / Browse / Beverages), then the default value of 0 will be transferred to the action method, which makes the code a bit more compressed and readable.
Results
There are quite a lot of new features in C # and VB. The above two are only a small part of them.
If you are looking for a good book that contains a description of all the new features in C # (including C # 4.0), as well as a description of the main libraries of .NET classes, then read the recently published O'Reilly book "
C # 4.0 in a Nutshell
