C # graphic tutorial-method parameter notes (Part 1 ),

Source: Internet
Author: User

C # graphic tutorial-method parameter notes (Part 1 ),

 

Four years later, the University was about to go through, and many skill points were out of order. As a result, every skill was only in the entry stage. In order to take C # As my main skill, I have prepared the relevant posture (knowledge) of the negative complementing, and found the book "C # graphic tutorial" through recommendations from various technical forums. In the first day, I felt that I only listed the knowledge, and many of them were known. But later I found that it made a lot of previous vague knowledge clear, I have also learned new things that I have never learned before. Especially with the help of pictures, it makes it easier for beginners to understand.

Not to mention, I want to sort out the parameter-related knowledge in Chapter 5 of this book.

 

1. Parameter knowledge points

The parameter types include value parameter, reference parameter, output parameter, and parameter array. The preceding parameters are both positional parameters. In addition, C # also provides named parameters and optional parameters (similar to python ).

 

2. Value Parameters

Using Value parameters, data is transmitted to the method by copying the values of real parameters to the form parameters. The following operations are performed when a method is called:

1. Allocate space for the form parameter in the stack

2. Copy the value of the real parameter to the form parameter.

Let's take an example and look at the Code:

The result is as follows:

Well, Let's explain how it is executed in the stack in an illustration:

In a word:If a value is assigned to a parameter in a method (a value is assigned to a member inside the reference type), the value type parameter remains unchanged after the method jumps out, and the value of the reference type parameter changes.

 

3. Reference parameters

Use the reference parameter.Two points:

1. the def modifier must be used in the method declaration and call;

2. Real parameters must be variables and must be assigned a value before being used as real parameters. If it is a reference type variable, you can assign a reference or null value.

For value parameters, the system allocates memory for the parameters on the stack. The referenced parameter has the followingTwo features:

1. No memory space is allocated for the form parameter on the stack;

2. The parameter name of the parameter is used as the real parameter alias and points to the same memory location.

Let's take an example and look at the Code:

The result is as follows:

Well, Let's explain how it is executed in the stack in an illustration:

 

In a word:If a value is assigned to a parameter in a method, the value type parameter andReference Type parameter valuesWill change.

 

3. The reference type is used as the value parameter and reference parameter.

In the previous two examples, both the reference type as the value parameter and the reference parameter result will change. However, it should be noted that all the changes are the values of the internal members of the reference type.

Now let's take a look at what will happen if a new object is assigned to the reference type itself. I think it is easy to know through the above two images.

The following code shows the reference typeValue ParameterTransfer:

The result is as follows:

Well, Let's explain how it is executed in the stack in an illustration:

The following code shows the reference typeReference parametersPass (only ref is added ):

The result is as follows:

Well, Let's explain how it is executed in the stack in an illustration:

Summary: in essence, the value parameter actually copies a real parameter on the stack to give the parameter a copy. The parameter is operated inside the method. After the method is executed, it is automatically destroyed on the stack, real parameters are not affected. The referenced parameter is equivalent to the alias of the real parameter. The method performs operations on the real parameter internally. After the method is executed, the real parameter is affected.

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.