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)
Copy codeThe Code is as follows:
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) will be 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 after entering the method (function) to make itself 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 declaration is valid:
Copy codeThe Code is as follows:
Class MyClass
{
Public void MyMethod (int I) {I = 10 ;}
Public void MyMethod (out int I) {I = 10 ;}
}
The following overload statement is invalid:
Copy codeThe Code is as follows:
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 attached
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:
Copy codeThe Code is as follows:
Class MyClass
{
Public void MyMethod (int I) {I = 10 ;}
Public void MyMethod (ref int I) {I = 10 ;}
}
However, the following overload statement is invalid:
Copy codeThe Code is as follows:
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
Copy codeThe Code is as follows:
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 ";
}