Parameter modifiers for methods in C #

Source: Internet
Author: User

Do the project for a long time, sometimes really need to calm down to seriously summarize their use of technology, instead of daily according to gourd painting scoop, every day busy, in the end do not know what they are busy, learned what, how much knowledge they have mastered. So I want to review the basic knowledge of C #, summarize the important knowledge into a point to record, convenient to read quickly later.

Parameters and parameter modifiers for the method:

1. (none). If a parameter is not marked with a parameter modifier, it is considered to be passed by value, which means that the method being called receives a copy of the original data.

2. Out: The output parameter is assigned by the called method, so it is passed by reference, and if the called method does not assign a value to the output parameter, a compilation error occurs. the biggest use of out is that callers can get multiple return values using only one call of the method.

3. Ref: The caller assigns an initial value and can be optionally re-assigned by the called method (the data is passed by reference). If the called method fails to assign a value to the ref parameter, there is no compiler error.

4. Params: This parameter modifier allows a variable number of parameters to be passed as a separate logical parameter, only one params modifier, and must be the last parameter of the method.

5. Optional parameters: This technique allows the caller of the method not to specify unnecessary arguments, but instead uses the default value of the parameter, and the value assigned to the parameter must be determined at compile time, not at run time, or a compilation error occurs. An optional parameter must be placed at the end of the method signature and a compile-time error occurs before an optional parameter is placed in a non-optional parameter.

6. Call a method with a named parameter: A named parameter allows you to specify the value of the parameter in any order when the method is called, so you can use the colon operator to specify the argument without having to pass the argument by position. In addition, named parameters must be placed at the end of a method call, and optional and named parameters are often used together.

Parameter modifiers for methods in C #

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.