Complete parsing of parameter passing in C #

Source: Internet
Author: User

I. Introduction

For some beginners (including those who have been working for several years), the problem of parameter passing between methods is sometimes confusing, because the basic questions for parameter passing are also frequently encountered during the interview process, such interview questions mainly check whether the developer base is solid, do you have a deep understanding of the C # median type and reference type? This is not a simple definition description of them, but a distribution in memory. Therefore, this article will give you an in-depth analysis of the parameter passing problems in C # and share my own understanding, you can analyze the results of passing parameters without running the program.

Ii. Pass by value

For parameter passing in C #, there are four types of parameters:

  • Value Type parameter passing by value
  • Pass reference type parameters by value
  • Pass value type parameters by reference
  • Transmit parameters of reference type by reference

However, by default, parameters in CLR methods are passed by value. To help you fully understand the transmission of parameters, we will analyze these four situations one by one.

2.1 value type parameter passing by value

For parameters, they are divided into the form parameters and real parameters. The form parameters refer to the parameters in the called method and the real parameters refer to the parameters of the called method. The following code helps you understand the concept of the form parameters and real parameters:

  Main( addNum =            Add(= addnum + 

To prove this, you can see the following code and running results:

   Main(            Console.WriteLine( addNum =            Add(= addnum + 

The running result is:

It can be seen from the results that the value of addNum has not changed after the method is called. The call of the Add method only changes the value of the addNum copy, so the value of addnum is changed to 2. However, our analysis is not over yet. In order to give you a deep understanding of transfer, we need to know why the value-based transfer of a value type parameter does not modify the value of a real parameter, I believe the following figure can explain all your doubts:

For this reason, it may be difficult to understand the text description. The following code and analysis diagram are used to help you understand:

  Main(            RefClass refClass =           +=   addnum=

The running result is:

Why does passing a reference modify the value of the original real parameter? For this, we will explain the parameter distribution in the memory:

Main (str = ChangeStr (=

The running result is:

Why is the original value not changed?. So the running result will be like the above. The following memory distribution chart can help you better understand the text expression:

CLR allows out or ref parameters to implement method overload Add (

Transferring by reference can solve the problem that the reference copy is changed when the value is passed without affecting the reference itself. In this case, the reference (that is, the address of the address) is passed ), instead of reference copies ). The Code passed by reference is as follows:

Main (pass by reference num = refStr = pass ChangeByValue by reference (= changeByRef (=

The running result is:

From the running results, we can see that the value of the reference itself has also been changed. The following figure helps you understand the method of transferring by reference:

Iv. Summary

Here, all the parameters are passed. In short,.

 

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.