Parse the essence of ref and out in. net

Source: Internet
Author: User

Http://www.cnblogs.com/davyli/archive/2008/11/01/1324352.html

 

It may be that when the relationship between value type and reference type in. Net encounters passing parameters to the function, the concept of passing by value and reference will pop up in our mind. If you see the following function (Code1) We will add a ref to the parameter in a conditioned reflection so that the parameter value can be modified inside the function.

// Code 1

Void change (int A, int B)
{
Int TMP =;
A = B;
B = TMP
}

OK. Check the following code.

// Code 2

Void Change2 (Object O)
{
O = new object ();
}

Static void main ()
{
Object OBJ = NULL;
Change2 (OBJ );
Console. writeline (OBJ ?? "Null ");
}

Null? Is object a reference type? Why does the parameter value not change when assigning values to the reference type in the function? Because there is only one answer: "All parameters are passed by value ". Do not be confused by the value type or reference type, or by the ref or out keyword. If you understand C or C ++, this problem is well understood. The so-called ref and out keywords (the two keywords are the same at the Il level, it is only syntactically required that the ref-modified parameter must be assigned a value. The out-modified parameter may not be assigned a value. To differentiate out semantics) for C, ref Int = int * and ref object = Object **, for C ++, ref Int = Int & ref object = object *&. if you do not understand C and C ++, it does not matter. I will describe the nature of ref and out through disassembly. The following test code is available:

// Code 3

Static void ref_out (ref int A, out int B, int C)
{
A = 15;
B = 127;
C = 99;
}
Static void main ()
{
Int I1 =-1; // ref must have an initial value
Int I2; // out not required
Ref_out (ref I1, out I2, i1 );
Console. writeline (i1.tostring () + "" + i2.tostring (); // 15 127
}

 

OK let's runProgramIn the above code line 12, set the breakpoint. Run the program to the breakpoint and check the current Register status

EBP = 0012f40 (stack bottom) ESP = 0012f440 (stack top) EIP = 0103009d

At this time, we step into the ref_out function and set the breakpoint in line 03. At this time, the register status is

EBP = 0012f434 ESP = 0012f3f4 EIP = 010300ab ECx = 0012f444 edX = 0012f440 ESI = 0012f440 EDI = 0012f444

Then let's look at the current status of the stack (via the clrstack-a command of SOS)

! Clrstack-
PDB symbol for mscorwks. dll not loaded
OS thread ID: 0x12c (300)
ESP EIP
0012f3f4 0103011d test_console.class1.ref_out (int32 byref, int32 byref, int32)
Parameters:
A = 0x0012f444
B = 0x0012f440
C = 0 xffffffff

0012f440 010300ad test_console.class1.main ()
Locals:
0x0012f444 = 0 xffffffff
0x0012f440 = 0x00000000

0012f69c 79e88f63 [gcframe: 0012f69c]

We can see that the value of A and B in the ref_out parameter is the stack address of the I1 and I2 local variables in the main function. Therefore, the out modifier is used to get the variable address and pass it to the ref_out method. The value of parameter C is the value of I1 (-1 ). Therefore, parameters are passed by value, that is, copy and transfer. Let's take a look at the situation at the bottom of the stack (EBP ).

0x0012f434: 0012f0000010300ad ffffff (offset = 8, parameter C)

These three values are the EBP values when the main function is running, the next instruction address after the ref_out function is called, and the value of parameter C (parameters A and B are composed of ECx, passed in and saved by EDX). In this case, we can view the disassembly of the ref_out function.

 

A = 15; // obtain the address saved in a and assign 15 to the i1 variable on the address.
00000026 mov dword ptr [EDI], 0fh
B = 127; // obtain the address saved in B and assign 127 to the i2 variable on the address.
2017002c mov dword ptr [esi], 7fh
C = 99; // The data on 0x0012f434 + 8 is ffffffff, that is, the location of C.
00000032 mov dword ptr [EBP + 8], 63 h
Then we set the breakpoint to the code line 07. At this time, we can check the bottom of the stack again.
0x0012f434 0012f0000010300ad 00000063
In this case, I1 and I2 are
0012f440 010300ad test_console.class1.main ()
Locals:
0x0012f444 = 0x0000000f -- 15
0x0012f440 = 0x0000007f -- 127

We can exit the ref_out method, and the Register status is

EBP = 0012f133 (this value is the value of the address indicated by EBP when the ref_out method is executed) ESP = 0012f440 EIP = 010300ad

Through the above careful analysis, you should have understood why the method in Code 2 cannot change the value of parameter o, even if it is a reference type. Still not clear? OK, I will continue, because in the Change2 method, only the address of the object OBJ on the hosting stack is saved, it is equivalent to saving the OBJ address (0x00000000) on [EBP + 8], and operating o = new object (); only assign the new object address to the [EBP + 8] position, so after the function call is complete, the obj is still null. In terms of syntax, adding and not adding ref, and out are the same in operation parameters. Therefore, we must understand the underlying operating mechanism to have a deep understanding of the. NET syntax.

 

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

 

C # has three keywords-Ref, out, And Params. Although I do not like these three keywords, they are suspected to damage the object-oriented feature. But since M $ is integrated into the C # system, let's take a look at the parameter modifiers ref, out, Params, and their differences.

No.1 Params
A keyword that allows a method (function) to have variable parameters.

Principle: no other parameters are allowed after the Params keyword in the method declaration, and only one Params keyword is allowed in the method declaration.

Example (copy to vs2005 for use, which is not described below)
Public partial class form1: Form
...{
Public static void useparams (Params int [] list)
...{
String temp = "";
For (INT I = 0; I <list. length; I ++)
Temp = temp + "" + list [I]. tostring ();
MessageBox. Show (temp );
}

Public static void useparams2 (Params object [] list)
...{
String temp = "";
For (INT I = 0; I <list. length; I ++)
Temp = temp + "" + list [I]. tostring ();
MessageBox. Show (temp );
}

Public form1 ()
...{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
...{
Useparams (1, 2, 3); // three parameters are displayed.
Useparams (1, 2); // check that there are 2 parameters. It can be changed.

Useparams2 (1, 'A', "test ");

Int [] myarray = new int [3]... {10, 11, 12 };
Useparams (myarray); // it can also be a container class, variable :)
}
}

No. 2 out
This is a reference transfer L.
Principle 1: When a method (function) uses out as a parameter, any changes made to the out parameter in the method (function) are reflected in this variable.
Principle 2: It is very useful to declare the out method when you want the method to return multiple values. You can still return a value using the out parameter. A method can have more than one out parameter.
Principle 3: To use the out parameter, the parameter must be passed to the method as the out parameter explicitly. The value of the out parameter is not passed to the out parameter.
Principle 4: you do not need to initialize the variable passed as the out parameter, because the out parameter clears itself when it enters the method (function) and turns itself into a clean parameter, for this reason, the out parameter must be assigned a value before the method is returned.. net ).
Principle 5: attributes are not variables and cannot be passed as out parameters.
Principle 6: if the declaration of the two methods is only different from that of the out method, the overload will occur. However, it is not possible to define a unique reload for ref and out. For example, the following overload statement is valid:
class myclass
{< br> Public void mymethod (int I) {I = 10 ;}
Public void mymethod (Out int I) {I = 10 ;}< BR >}< br> The following overload declaration is invalid:
class myclass
{< br> Public void mymethod (Out int I) {I = 10 ;}< br> Public void mymethod (ref int I) {I = 10 ;}< BR >}< br> for information about passing arrays, see passing Arrays Using ref and out.
attached example

No. 2 ref
Ref is just an address !!!
Principle 1: When a method (function) uses ref as a parameter, any changes made to the ref parameter in the method (function) will be reflected in the variable.
Principle 2: When a method is called, any changes made to the parameters in the method will be reflected in this variable.
Principle 3: To use the ref parameter, you must pass the parameter as the ref parameter to the method explicitly. The ref parameter value can be passed to the ref parameter.
Principle 4: The variables passed by the ref parameter must be initialized, because the ref parameter still points to the original value after it enters the method (function, for this reason, the ref parameter can also be used internally without any operation.
Principle 6: if the declarations of the two methods are only different in their use of ref, the overload will occur. However, it is not possible to define a unique reload for ref and out. For example, the following overload declaration is valid:
Class myclass
{
Public void mymethod (int I) {I = 10 ;}
Public void mymethod (ref int I) {I = 10 ;}
}
However, the following overload statement is invalid:
Class myclass
{
Public void mymethod (Out int I) {I = 10 ;}
Public void mymethod (ref int I) {I = 10 ;}
}
For information on passing arrays, see passing Arrays Using ref and out.
Example

Public static string testout (out string I)
...{
I = "out B ";
Return "Return Value ";
}

Public static void testref (ref string I)
...{
// Change the Parameter
I = "Ref B ";
}

Public static void testnoref (string refi)
...{
// No need to change anything. This is too obvious.
Refi = "on C ";
}

Public form1 ()
...{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
...{
String Outi; // initialization not required
MessageBox. Show (testout (Out Outi); // Return Value
// Output "Return Value ";
MessageBox. Show (Outi); // The out parameter after the call
// Output "out B ";

String refi = "A"; // initialization required
Testref (ref refi); // call Parameters
MessageBox. Show (refi );
// Output "Ref B ";
Testnoref (refi); // do not use ref
MessageBox. Show (refi );
// Output "Ref B ";
}

 

Bytes ------------------------------------------------------------------------------------------------------------------

In C #, parameters can be passed either by value or by reference. You can pass a parameter through reference to allow function members to change the parameter value and keep the change. To pass parameters through references, you can use the ref or out keyword. Both the ref and out keywords provide similar functions, and they also act like pointer variables in C. Their differences are:

1. When using ref parameters, the input parameters must be initialized first. For out, initialization must be completed in the method.

2. When using ref and out, the ref or out keyword must be added to both the method parameters and the execution method. To match.

3. Out is suitable for use where multiple retrun return values are required, while ref is used when the caller's reference is modified by the method to be called.

Note: in C #, there are four types of method parameters passed: by value, by reference, and output parameter (by output ), array parameters (by array ). There is no additional modifier for the value passing parameter. The address passing parameter requires the modifier ref, the output parameter requires the modifier out, And the array parameter requires the modifier Params. If the value of the parameter is changed during a method call, the parameter passed in to the method does not change after the method call is completed, but retains the original value. On the contrary, if the value of the parameter is changed during the method call process, the parameters for passing in the method also change after the call is completed. In fact, we can clearly see the meaning of the two from the name-the pass-through parameter transfers a copy of the call parameter, while the transfer parameter passes the memory address of the call parameter, this parameter points to the same storage location inside and outside the method.

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.