C # value transfer and reference transfer of Study Notes (basic knowledge Review,

Source: Internet
Author: User

C # value transfer and reference transfer of Study Notes (basic knowledge Review,
I. To learn about value transfer and reference transfer, you must first understand the meanings of these two types. refer to the previous article.

C # value type and reference type of study notes (basic knowledge Review)

2. PASS Parameters to methods are divided into value transfer and reference transfer. 2.1 When a variable is passed to a method through reference, the called method gets this variable. More accurately, it is a pointer to a variable in the memory. Therefore, any changes made to the variables in the method remain valid after the method exits. If a variable is passed to the method through a value, the called method returns an identical copy of the variable. That is to say, after the method exits, modifications to the variable will be lost.   2.2 pass the value type: the variable of the value type contains actual data, and the parameter transmits a copy of the data itself. As follows, the int type is passed to the method through a value. Any changes made to this value by the corresponding method do not change the value of the original int object.
2.3 pass the value type through reference: it is not a value of the value type, but a reference to the value type, as shown below. Parameter I is not of the int type, but a reference to num, when I is added to the method, it is actually the num referenced by I. So I added the num value. (In my opinion, AddOne (ref num) is equivalent to passing the num pointer 104524728, while the operation on I in the AddOne method is equivalent to the value pointed to by the Operation pointer, that is, the value of num ).

 

 

 

2.4: The reference type is passed through the value. Variables of the reference type do not directly contain their data, but contain references to the data. When a parameter of the reference type is passed through a value, the data value pointed to by the reference is changed, but the value of the reference itself cannot be changed. The sample code is as follows:2.4.1: The following example shows how to pass parameters of the reference type to the AddOne method by using values. Str. Because this parameter is a reference to str, operations in the AddOne method change the value of str.

 

Class Program {static void Main (string [] args) {StringBuilder str = new StringBuilder (); str. append ("hello"); AddOne (str); Console. writeLine ("num value:" + str); // the output value is hello, word Console. readKey ();} public static void AddOne (StringBuilder sb) {sb. append (", world ");}}

 

 

 

2.4.2 when the reference type is passed through the value, this operation is only valid in the method when trying to re-allocate the parameter to different memory locations, and does not affect the original variables. The following code:

 

Class Program {static void Main (string [] args) {StringBuilder str = new StringBuilder (); str. append ("hello"); AddOne (str); Console. writeLine ("str value:" + str); // the output value is hello Console. readKey ();} public static void AddOne (StringBuilder sb) {sb = new StringBuilder (); sb. append (", world ");}}
View Code

 

 

 

2.5: Pass the reference type through reference. It is actually a reference to pass its reference address, similar to a pointer. If a new instance is created, the original reference type is affected. The Code is as follows: 
Class Program {static void Main (string [] args) {StringBuilder str = new StringBuilder (); str. append ("hello"); AddOne (ref str); Console. writeLine ("str value:" + str); // the output value is, world Console. readKey ();} public static void AddOne (ref StringBuilder sb) {sb = new StringBuilder (); sb. append (", world ");}}
View Code

 

 

Iii. Conversion of value types and reference types can be found in the next article:

C # conversion of value types and reference types in Study Notes (basic knowledge Review) (packing and unpacking)

 


 


 

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.