Optional parameters and named arguments in C # 4
- Transfer
This is the seventeenth article in the series , dedicated to the release of VS 2010 and .NET 4. Today's post will cover the topic of new language features added in C # 4.0 - optional parameters and named arguments, as well as a 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 knew how to do this a long time ago).
Parameters are optional when a 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 you call the above method, we can pass two parameters explicitly: Or omit the second optional parameter, in this case the default value 0 will be passed: Please note 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 to 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 the method in any of the ways listed:
Since both parameters are optional, and in the case when 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 where we can use the optional parameters in ASP.NET MVC 2 to communicate with the action methods in controller classes.
For example, we want to bind URLs such as “Products / Browse / Beverages” or “Products / Browse / Deserts” to the controller’s action method. We can implement this by writing a URL router that associates the URLs with the method: We can use the value of the optional parameter of the query string “page”, or we can 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, as usual, you 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, 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: C ASP.NET MVC 2, you can use the optional parameters supported in VB and C # for a shorter and clearer implementation. It is enough to declare an action method parameter as optional with a default value: C # VB
If the value “page” 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 line (/ 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 quite a few new features in C # and VB. The above two are just 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 core libraries of .NET classes, then read the recently released O'Reilly book " C # 4.0 in a Nutshell
