1. The out must be initialized in the function body. It does not make sense to initialize it outside.That is to say, an out-type parameter cannot obtain the initial value passed in outside the function body.
2. Ref must be initialized in vitro.
3. Any modification to both of them in the function body will affect the outside.
Example:
Using system;
Namespace consoleapplication1
{
Class C
{
Public static void reffun (ref string Str)
{
STR + = "fun ";
}
Public static void outfun (out string Str)
{
STR = "test"; // It must be initialized in the function body
STR + = "fun ";
}
}
Class class1
{
[Stathread]
Static void main (string [] ARGs)
{
String test1 = "test ";
String Test2; // No Initialization
C. reffun (ref test1); // correct
C. reffun (ref Test2); // error. Test2 is used if no value is assigned.
C. outfun (Out test1); // correct, but the value test is passed in.
C. outfun (Out Test2); // correct
Console. Read ();
}
}
}