Beginner's note (passing parameters of the C # method ),

Source: Internet
Author: User

Beginner's note (passing parameters of the C # method ),

When declaring a method, the defined parameters are formal parameters (hereinafter referred to as form parameters). The values of these parameters are transmitted by the caller, And the caller transmits actual data, when calling a method, you must specify the real parameters according to the parameter type and sequence defined by the method. The parameter passing of a method is divided into passing by value and passing by reference by nature.

1. PASS Parameters with security values

When passing by value, the value of the real parameter variable is assigned to the corresponding row parameter variable, that is, the called method only acceptsThe data value of the real parameter.When the data of the form parameter is changed inside the method, the value of the parameter variable is not affected, that is, the real parameter and the form parameter variable are two different variables, they have their own memory address and data value. Therefore, the real parameter passed to the form parameter isUnidirectional value transfer. Parameters of the value type are passed by default. Although string and object are referenced data, they have the effect of passing parameters by value.

2. Passing parameters by reference

As long as the method can return a value, in practice, it is often necessary for the method to return multiple values or modify the input parameter values and return them. In this case, only the return statement is not available, this can be achieved by passing parameters by reference.

When passing a reference parameter, the caller will pass the reference of the real parameter to the corresponding parameter. The reference of the real parameter represents the memory address of the data. Therefore, the form parameter and the real parameter point to the same reference. If the data value referenced by the form parameter is changed within the method, the data value referenced by the real parameter is also modified. When the value type and string type parameters must be passed as referencesRef keywordDeclare the referenced parameter. Whether it is a real parameter or a form parameter, you must addKeyword ref.

3. Output Parameters

Only one value can be returned for return in the method, and multiple values can be returned for reference parameters. However, parameters modified with ref must be initialized before being passed. However, sometimes the parameter is not determined before it is passed, and its value should be returned after the method call ends. In this case, you can use the output parameter, which does not need to be initialized for the real parameter, output parameters are used to return data in the method to the real parameter through the form parameter, but the value of the real parameter is not passed to the form parameter. A method can have multiple output parameters.

In C #,Out keywordTo declare the parameter, whether it is a real parameter or a form parameter, as long as it is an output parameter, you must add the out keyword.

Parameters modified with ref and out are passed parameters of reference type. Modifications and assignments within the method are stored in real parameters, but they are different when used.

(1) parameters modified with ref must be assigned a value before passing parameters through the method.

(2) For an out-modified parameter, you do not need to initialize the parameter before passing the parameter through the method. However, you must assign a value to the parameter in the method before using the parameter. Before the method ends, you must assign a value to the parameter.

4. parameter transfer of reference type

Parameters of the reference type are passed by reference. Therefore, when passing parameters of the reference type, the ref and out keywords (except string) are not required, in fact, the reference of the real parameter data is copied to the form parameter. Therefore, the parameters and real parameters point to the same memory area.

5. array parameters

Arrays are also reference data. When an array is passed as a parameter, it is also passed as a reference. However, an array can be used as a parameter in either of the following two forms:Params modifier,The other is that the params keyword is not added. When the params modifier is not added, the corresponding real parameter must be an array name. When the params modifier is added, the corresponding real parameter can be an array name or a list of array element values, the system automatically organizes the elements into an array. Either way, the parameter array cannot define the length of the array.

When using the params modifier, pay attention to the following points:

(1) params can modify any type of parameters.

(2) params can only modify one-dimensional arrays.

(3) methods cannot be overloaded only based on the params keyword.

(4) The ref and out keywords cannot be used for params arrays.

(5) The params array can only be in the last parameter (that is, each method can only have one params array ).

6. Method Overloading

Here is a brief introduction to method overloading. Important introduction features of method overloading:

(1) the names of the overload methods must be the same.

(2) The number or type of parameters of the method must be different.

When calling a method with overload, the system determines the most matched method call based on the number or type of parameters.


Do you have any C Language Study Notes or prepared some documents?

Hundred examples of C language programming: it is easier to learn from examples. It is better than simply reading those books with principles //
Is it a procedural test or a theoretical test?
Recommended program design guidance and online practices for trial use, outline-level books. Link: ai.pku.edu.cn/book/
The theoretical written examination must be dominated by Tan haoqiang, but it is indeed a bit messy. You may wish to buy a related exercise or something like this. Tan haoqiang has many related exercises in this book, which are basically the same, choose based on the existing materials on hand. We will not recommend it.

C language beginners, what programming software is better for laptops?

Vc6.0 this has a learning version of the Professional Edition of the Enterprise Edition
If you just want to learn, the learning version will usually be dozens of megabytes, and the enterprise edition will be several hundred megabytes

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.