C # Method passing parameters

Source: Internet
Author: User

First, the use of parameters:
1. Value parameter (values Parameter)
Format: Method name (parameter type parameter name [, parameter type parameter name])

2. Reference parameters (Reference Parameter)
Format: Method name (ref parameter type parameter name [, ref parameter type parameter name])

3. Output parameters (out Parameter)
Format: Method name (out parameter type parameter name [, out parameter type parameter name])

The difference between the value parameter and the reference parameter and the output parameter:
The value of the argument in the 2.1 value parameter is not changed with the change of the parameter value;
Parameter values are not affected, and when an argument is passed to a formal parameter, it is allocated another part of the space on the stack. In this way, two parameter changes are not affected.
2.2 The values of arguments in the reference and output parameters change with the parameter value
(Formal parameters: The parameters specified in the definition function, which do not account for memory cells when function calls are not present. Only when a function call occurs does the parameter in the function be assigned an inner memory element. After the call is finished, the cells that are part of the parameter are also freed. The value of the argument is assigned to the parameter when called);
In the reference and output parameters, the argument allocates space in the stack. When an argument is passed to a parameter, the address is passed to the formal parameter, which is the passed-in pointer, and the argument changes as the parameter is changed.

Three, the reference parameter and the output parameter difference:
3.1 When using ref and out parameters, both the parameter in the method and the calling parameter require the ref or out keyword
3.2 The result of using the ref parameter is the same as the out parameter, except that ref needs to assign an initial value to the parameter in the calling method before the call. Out does not need to assign an initial value to the parameters in the calling method before the call, and the parameters are initialized in the actual method body.

C # Method passing 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.