C # in principle ref and out of

Last year, CSDN written on, and now it moved here.

First, cause problems

Took so long ref and OUT , you really understand how they are involved in making real value parameter in sync do?

Second, the research premise

To examine this issue, the premise is to understand C # among the methods of how to pass parameters:

1.CLR supports two types: value types and reference types .

     a Value Type: values ​​are generally stored in the thread stack, as the field class object stored on the heap.

     reference type b: object instance stored on the heap, the references in the stack on the thread, boxed value type can be changed by a reference type.

  

1  // represents a reference type 
2  class Ref
 . 3  {
 . 4      Private  int the _x;
 . 5      public  int X-
 . 6      {
 . 7          GET => the _x;
 . 8          SET 
. 9          {
 10              the _x = value;
 . 11          }
 12 is       }
 13 is  }
 14  
15  static  void TestValAndRef ()
 16  {
 17      // first portion 
18 is      int A1 = . 5 ;
. 19      var REF1 = new new Ref ()
 20 is      {
 21 is          X-= . 5 
22 is      };
 23 is  
24      // second portion 
25      int A2 = A1;
 26 is      A2 = 10 ;
 27      var REF2 = REF1;
 28      ref2.X = 10 ;
 29 }

The code above is stored in the case where variables:

2 is divided into transmission parameters by value and pass references to two kinds.

3. For CLR, the use of out and ref produces the same IL code, and the metadata in addition to a 'bit (designated for the recording method is declared out or ref), the exactly the same.

 

 1 //测试ref
 2 static void TestRef(ref Ref r)
 3 {
 4     r = new Ref()
 5     {
 6         X = -1
 7     };
 8 }
 9 
10 //测试out
11 static void TestOut(out Ref r)
12 {
13     r = new Ref()
14     {
15         X = -1
16     };
17 }
18 
19 static void Main(string[] args)
20 {
21     var ref1 = new Ref()
22     {
23         X = 10
24     };
25 
26     TestRef(ref ref1);
27     TestOut(out Ref ref2);
28 }

The above code is compiled in IL:

It can be seen TestRef identical and corresponding method of IL TestOut!

4. In the CLR, process parameters and return values ​​are stored by the stack to things and these arguments while indicating parameter appears to coincide with, but is actually stored separately, i.e., formal parameters and arguments are two different variables.

Third, the research questions

All methods 1.CLR default parameters are passed by value way:

    a. For value type is passed a copy of the value. For example the value of the thread stack a1: 5 .

    b. For reference types, the transmission is a reference to the object, the reference itself is passed by value, a parameter to a reference stored up within the calling method, if the change is called internally method reference stored in the parameter (new a new endowed with its object or another object), then the parameter to cut off contact with the argument, the subsequent modification of the argument does not work; but make a change if the citations are not changed, the fact of arguments made changes. For example, the value of the thread stack ref1: Quote type of object.

2. When using a ref or out, C # by value becomes a pass by reference, similar to the C & a1, I think here & is the corresponding ref and out of it :

    a. For the type of value, it is passed a reference value (address value will be appreciated that, similar to the type of reference by value) => & parameter , remove & the remaining parameter is actually solid ginseng, so refer to this parameter saved will never be changed, that is, change is always the argument value. For example, a reference to the thread stack of a1.

    b. For reference types, it is passed a reference to the variable (can be understood as a reference point to an instance of an object reference stack address, the popular talk is referenced object is stored in an address on the stack, passing here for reference the address) =>   & parameter , thus ensuring that the argument inside the object calling the method used, rather than a copy reference, so any changes are made to change the argument. For example, a reference to the thread stack of ref1.

 

Omissions inevitable, if there is understanding in the wrong place, please leave a message below, thank you!

Guess you like

Origin www.cnblogs.com/xiaoxiaotank/p/11317517.html