I have been stuck in the difference between ref and out, but I can't find out. Today I finally have the opportunity to discuss this problem, so I don't hesitate to use the resources to find it.
Ref: Before calling such a method, you must initialize or assign a value to the ref parameter.
This ref parameter can be re-assigned within the method. Optional action
Out: Before calling such a method, you can initialize or assign a value to this parameter.
This out parameter must be re-assigned within the method. required action
The following content is from Google. The above content is converted into your own language. Although some cannot understand it
In general:
generally, we pass a value to the method . the method obtains a copy of these values , use these copies , after the method is run , these copies are discarded , the original value is not affected . In addition, we have other parameters passed to the method. , reference (REF) and output (out ).
sometimes , we need to change the value of the original variable , , we can pass a variable reference to a method , instead of the variable value . reference is a variable , it can access the value of the original variable , modifying reference changes the value of the original variable . variable values are stored in memory , you can create a reference , it points to the location of the variable in the memory . when the reference is modified , modify the value in the memory , therefore, the variable value can be modified . when we call a method containing referenced parameters , parameters in the method point to the corresponding variable passed to the method , SO , we will understand , Why does modification to the parameter variable result in the value of the original variable .
RefIndicates that the reference parameter is used in the method, and the reference parameter does not open up a new memory area. CompileProgramThe method is passed to the address in the memory.The referenced parameter is usually initialized.
For example
Int I = 1, j = 2 ;//Initialization
Swap (Ref I, ref J );
OutOutput parameters do not open up new memory areas, but unlike the reference parametersInitialize the variableOutput parameters are mainly used to pass data returned by methods.
String name, path ;//No Initialization
File (out name, out path );
First, let's startRef :( msdn)
C #Keywords|Method Parameters
TheRefThe method parameter keyword allows the method reference to pass to the same variable of the method. When the control is passed back to the call method, any changes made to the parameters in the method will be reflected in this variable. To useRefParameter.RefParameters are passed to the method explicitly.RefThe parameter value is passedRefParameters. PassRefThe parameter must be initialized first. Set this methodOutThe latter parameter is passedOutParameters do not need to be explicitly initialized before.
The attribute cannot be a variable.RefAndOutParameter transfer.
If the two methods are declared only whenRefIf the usage is different, the overload will occur. However, it cannot be defined only inRefAndOutDifferent overloading. 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 ;}
}
Example
Using system;
Public class myclass
{
Public static void testref (ref char I)
{
// The value of I will be changed in the calling Method
I = 'B ';
}
Public static void testnoref (char I)
{
// The value of I will be unchanged in the calling Method
I = 'C ';
}
Public static void Main ()
{
Char I = 'a'; // variable must be initialized
Testref (Ref I); // The ARG must be passed as REF
Console. writeline (I );
Testnoref (I );
Console. writeline (I );
}
}
Output
B
B
Here we will talk about it for youOut:
C #Keywords|Method Parameters
TheOutThe method parameter keyword allows the method reference to pass to the same variable of the method. When the control is passed back to the call method, any changes made to the parameters in the method will be reflected in this variable. When you want the method to return multiple values, declareOutThe method is very useful. UseOutThe parameter method can still return a value. There can be more than one methodOutParameters. To useOutParameter.OutParameters are passed to the method explicitly.OutThe parameter value is not passedOutParameters. No need to initializeOutThe variable passed by the parameter. However,OutParameter Value assignment.
If the two methods are declared only inOutIn different ways, the overload will occur. However, it cannot be defined only inRefAndOutDifferent overloading. For example, the following overload declaration is valid:
Class myclass
{
Public void mymethod (int I) {I = 10 ;}
Public void mymethod (Out int I) {I = 10 ;}
}
The following overload statement is invalid:
Class myclass
{
Public void mymethod (Out int I) {I = 10 ;}
Public void mymethod (ref int I) {I = 10 ;}
}
Using system;
Public class myclass
{
Public static int testout (Out char I)
{
I = 'B ';
Return-1;
}
Public static void Main ()
{
Char I; // variable need not be initialized
Console. writeline (testout (Out I ));
Console. writeline (I );
}
}
Output
-1
B
ParamsYou can specify the parameter when the number of parameters is variable.
In the method declarationParamsNo other parameters are allowed after the keyword, andOnly one method can be declaredParamsKeywords.
Using system;
Public class myclass
{
Public static void useparams (Params int [] list)
{
For (INT I = 0; I <list. length; I ++)
Console. writeline (list [I]);
Console. writeline ();
}
Public static void useparams2 (Params object [] list)
{
For (INT I = 0; I <list. length; I ++)
Console. writeline (list [I]);
Console. writeline ();
}
Public static void main ()
{
Useparams (1, 2, 3 );
Useparams2 (1, 'A', "test ");
Int [] myarray = new int [3] {10, 11, 12 };
Useparams (myarray );
}
}
Output
1
2
3
1
A
Test
10
11
12