Optional parameters in the. NET in full bloom

Source: Internet
Author: User
Keywords Can function for if
Tags .net applications asp asp.net code data example function

. NET always supports optional parameters, but the C # language does not support that optional parameters are usually prohibited except for invoking a COM library. Now, with the support of C # 4 for optional parameters, we can see that optional parameters are being used more and more, not just in legacy code. These applications include interoperability with dynamic languages, read-only data types, and many places in asp.net mvc.

Obviously, COM is still the main option for optional parameters. For example, when you use the Office library, a function may have more than 20 parameters, and it is inconvenient to manually specify each parameter. If there is no secondary tag, you can only use the number of parameters to determine which value corresponds to which parameter, which is very error-prone. For people unfamiliar with COM, you can imagine the same feeling that the previous mainstream language did not support the function overload. Because functions that contain optional parameters are not different from normal functions, this problem can be solved well.

In addition to COM, the C # team also uses C # 4 as a late-bound way to support the DLR. In fact, this is the real purpose of the optional parameters, others are incidental convenience. Optional parameters are necessary because dynamic languages do not have an explicit type declaration and cannot be overloaded based on parameter types.

With the growth of multi-core and multiprocessor computers, more and more discussions are being devoted to synchronization and concurrency. Read-only data types and how to define them have become a common topic. If each field in the type needs to be set, the standard constructor solves the problem. But if most of the fields in a type are optional, it's more cumbersome. Overloaded constructors can solve some of the problems, and for people who use Java or older versions of C #, they may be more inclined to use the builder pattern. But if you have optional arguments, you need only one constructor to solve the problem. Controller in asp.net MVC 2 You can now set a default value for the query string parameter. Although you can also use attribute to set, but optional parameters are the most concise way.

Microsoft Razor The new view engine for asp.net mvc, using optional parameters in the helper method. This allows the helper to include more options while also supporting a custom coding style. In addition, Razor automatically converts an expression to a delegate without requiring the use of the syntax of a lambda or anonymous delegate.

There are also places where we never want to use optional parameters. The CLS defines a subset of the CLR, all. NET language to support this subset. The CLS is clearly defined and cannot rely on optional parameters. This means that the optional parameters will not appear in BCL and may not appear in any future class libraries contained in the. NET framework. But for the unofficial class library on CodePlex, this rule does not need to be strictly adhered to.

View English Original: Optional Parameters Are gaining ups in. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.