Re-learning C # Series-01. Method Parameters

Source: Internet
Author: User

Let's look at two concepts before you introduce them:Formal ParametersAndactual Parameters。

1. the formal parameter declaration is in the parameter list of the method, as in the following code, x and y are formal parameters.

2. An argument is an expression or variable used for the initial taxiing parameter, such as the following code. 5 and age are method arguments, 5 is an expression, and age is a variable of type int.

int age=; MethodName (5, age);
The method parameters in C # can be divided into: value parameter, reference parameter, output parameter and parameter array.

1 . Value parameter: pass the data to the method by copying The value of the argument to the formal parameter. When the method is called, the system follows: 1) Allocates space for the parameter in the stack, and 2) copies the value of the argument to the parameter. as follows.

Note: Do not confuse value types with value parameters, only arguments are the values of the arguments that are copied to the parameter, regardless of whether the argument type is a reference type or a value type.

2. reference parameter : the ref modifier must be used in method declarations and invocations.

Note: As a reference parameter, the system does not allocate memory for the number of shapes on the stack, and the parameter names of the parameters are aliases of the variables that are arguments, pointing to the same memory location as the arguments.

3. output parameters : Used to pass data from the method body to the calling code, you must use the Out modifier in both the declaration and the call.

Note: An output parameter must be assigned before it is called in the method body, meaning that any assignment outside the method body is meaningless.

4. parameter array : Use the params modifier before the data type.

1) Single value incoming method:

2) as a whole array of incoming methods:

Note: There can be only one parameter array in a method, and must be the last parameter of the parameter list.

Re-learning C # Series-01. Method Parameters

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.