Objective C # Reading Notes-Entry 10: use optional parameters to reduce the number of methods to be overloaded

Source: Internet
Author: User

C #4.0IntroducedName Parameter(Msdn translates the name as "named real parameter". I personally think that the name parameter is more vivid, and the optional parameter is also) andOptional Parameter(Optional ). ClientCodeUsing a named parameter means that the parameter name in the method is also part of the public interface. If you modify the name of a public parameter, the caller's code may be damaged. This means that the caller should try to avoid using the named parameter. As the API designer, the caller should also avoid modifying the parameter names in the public-goods protected method.

Of course, it does not mean that the name parameter is useless, and it also has its own applicable scenarios. The combination of named parameters and optional parameters can simplify the calling Syntax of many APIs, especially the com api of Microsoft Office. As follows:

 1               VaR Wasted = Type. missing;  2               VaR Wordapp = New  Microsoft. Office. InterOP. Word. Application (); 3 Wordapp. Visible = True  ;  4   5 Documents docs = Wordapp. documents;  6 Document Doc = docs. Add ( Ref Wasted, Ref Wasted, Ref  Wasted );  7   8 Range = Doc. Range (0 , 0  );  9 Range. insertafter ( "  Testing, testing, testing...  " );

 

Any office InterOP ApplicationProgramThe type. Missing object must be used multiple times. The meaningless code masks the core logic.Optional ParameterAndName ParameterThe main reason is. After an optional parameter is added, the Office API will create a default value for a place where type. Missing may be used. The above code can be simplified as follows:

 1               VaR Wordapp = New Microsoft. Office. InterOP. Word. Application ();  2 Wordapp. Visible = True  ;  3   4 Documents docs = Wordapp. documents;  5 Document Doc = Docs. Add ();  6   7 Range = Doc. Range ( 0 , 0 );  8 Range. insertafter ( "  Testing, testing, testing...  " );

 

We can see this small modification.Enhanced code readability. Now, if you want to create a new web page instead of a Word document, and then the last parameter of the add () method, you can use the name parameter to specify the last parameter:

 1               VaR Wordapp = New  Microsoft. Office. InterOP. Word. Application ();  2 Wordapp. Visible = True ;  3 Documents docs = Wordapp. documents;  4   5               Object Doctype = Wdnewdocumenttype. wdnewwebpage;  6 Document Doc = docs. Add (documenttype: Ref  Doctype );  7   8 Range = Doc. Range ( 0 , 0 );  9 Range. insertafter ( "  Testing, testing, testing...  " );

The specific meaning of the name parameter is:For APIs that provide default slave parameters, you can only provide the parameters to be used.Using a name parameter is much easier than using multiple overload parameters. The add method named parameter in the sixth row in the above example uses the ref keyword, and C #4.0 is used in programming in the com scenario. The ref parameter is also Optional: because com itself transmits objects through reference, almost all parameters are passed in the form of reference, even if these parameters are not modified by the called method.

In collaborative development, if your code needs to be called by others, whether you agree or not, your API users can use the named parameter to call your method anywhere, so you mustThe parameter name is also part of the public interface.. Modifying the parameter name may cause the client code to fail compilation.

Section:

For the first release of an assembly, you can use optional and named parameters at will, and provide the required overload. However, in subsequent releases, you must create an overload for additional parameters. In this way, the existing program can still run normally, and you should avoid modifying the parameter name in subsequent releases, because the parameter name and become part of the public interface.

 

References & further reading

named real parameters and optional real parameters

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.