The difference between ref and out is that in C #, parameters can be passed either by value or by reference. Passing parameters by reference allows a function member to change the value of a parameter and keep the change. To pass parameters by reference, you can use the ref or out keyword. The two keywords, ref and out, can provide similar effects and act like pointer variables in C. The difference between them is:
1. When using a ref parameter, the passed-in parameter must first be initialized. For out, initialization must be done in the method.
2. When using ref and out, the ref or out keyword is added to the method's parameters and execution methods. To meet the match.
3. Out is suitable for use where multiple return values need to be retrun, while ref is used to modify the caller's reference in a method that needs to be called.
Note: In C #, the parameters of a method are passed in four types: Pass value (by value), address (by reference), output parameter (by output), array parameter (by array). The pass-through parameter requires no additional modifiers, the address parameter needs a modifier ref, the output parameter requires a modifier out, and the array parameter requires a modifier params. If the value of the parameter is changed during a method call, the parameters of the passed-in method do not change after the method call is complete, but instead retain the value of the original pass-in. On the contrary, if the method invocation procedure changes the value of the parameter, the parameters of the incoming method are changed after the call is completed. In fact, from the name we can clearly see the meaning of the two--the pass parameter is passed a copy of the invocation parameter, and the address parameter is passed the memory of the calling parameter, which is the same storage location inside and outside the method.
The ref method parameter keyword on the method parameter causes the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.
To use the ref parameter, you must explicitly pass the parameter to the method as a ref parameter. The value of the ref parameter is passed to the ref parameter.
The arguments passed to the ref parameter must be initialized first. Comparing this method to an out parameter, the latter's arguments do not have to be explicitly initialized before they are passed to the out parameter.
property is not a variable and cannot be passed as a ref parameter.
If the declarations of both methods differ only in their use of ref, an overload occurs. However, you cannot define overloads that differ only in respect of ref and out.
Out
The Out method parameter keyword on the method parameter causes the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.
Declaring an out method is useful when you want a method to return more than one value. Methods that use out parameters can still return a value. A method can have more than one out parameter.
To use an out parameter, you must explicitly pass the parameter to the method as an out parameter. The value of the out parameter is not passed to the out parameter.
You do not have to initialize a variable passed as an out parameter. However, you must assign a value to the out parameter before the method returns.
property is not a variable and cannot be passed as an out parameter.
There are many articles on the internet saying that ref is only a value, out of the address, and so on, it seems not very accurate. Here is the example code I made, you can try it out:
public int refvalue (int i,ref int J)
{
int k = J;
j = 222;
return i+k;
}
public int Outvalue (int i, out int j)
{
j = 222;
return i + j;
}
private void Cmdref_click (object sender, EventArgs e)
{
int m = 0;
MessageBox.Show (Refvalue (1, ref m). ToString ());
MessageBox.Show (m.tostring ());
}
private void Cmdout_click (object sender, EventArgs e)
{
int m;
MessageBox.Show (Outvalue (1, out m). ToString ());
MessageBox.Show (M.tostring ());
}
In a sentence that is summed up online, ref is there, and out is only out.
Original address:http://www.cnblogs.com/gjahead/archive/2008/02/28/1084871.html
C # Fundamentals: The difference between ref and out [go]