asp.net of c ref out params

  • 2020-05-07 19:27:38
  • OfStack

NO.1 params
A keyword that allows a method (function) to have variable arguments.
Principle: no other arguments 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)
 
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 button1_Click(object sender, EventArgs e) 
{ 
UseParams(1, 2, 3);// The parameter is 3 a  
UseParams(1, 2); // The parameter is 2 Alpha, variable  
UseParams2(1, 'a', "test"); 
int[] myarray = new int[3] { 10, 11, 12 }; 
UseParams(myarray); // See also can be a container class, mutable :)  
} 
} 

NO.2 out
This is 1 reference passing L.
Principle 1: when a method (function) USES out as an argument, any changes made to the out argument in the method (function) are reflected in the variable.
Principle 2: declaring an out method is useful when you want a method to return multiple values. Methods that use the out parameter can still return one value. One method can have more than one out parameter.
Principle 3: to use the 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.
Principle 4: you don't need to initialize the variable passed as the out parameter, because the out parameter clears itself as a clean parameter when it enters the method (function), and for this reason you must assign a value to the out parameter before the method returns.
Principle 5: attributes are not variables and cannot be passed as out parameters.
Principle 6: overloading occurs if the declarations of two methods differ only in the use of out. However, it is not possible to define overloads that differ only in terms of ref and out. 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 declaration is invalid:
 
class MyClass 
{ 
public void MyMethod(out int i) {i = 10;} 
public void MyMethod(ref int i) {i = 10;} 
} 

For information about passing arrays, see passing arrays using ref and out.
Sample attached below
NO.2 ref
ref is just one address!!
Principle 1: when a method (function) USES ref as an argument, any changes made to the ref argument in the method (function) are reflected in the variable.
Principle 2: when a method is invoked, any changes made to parameters in the method are reflected in that variable.
Principle 3: to use the ref parameter, you must explicitly pass the parameter to the method as an ref parameter. The value of the ref parameter can be passed to the ref parameter.
Principle 4: the variable passed by the ref parameter must be initialized, because the ref parameter is still itself when it enters the method (function), its address is still pointing to the original value, and for this reason the ref parameter can also not operate within the method using it.
Principle 6: overloading occurs if the declarations of the two methods differ only in their use of ref. However, it is not possible to define overloads that differ only in terms of 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;} 
} 

But the following overload declaration is invalid:
 
class MyClass 
{ 
public void MyMethod(out int i) {i = 10;} 
public void MyMethod(ref int i) {i = 10;} 
} 

For information about passing arrays, see passing arrays using ref and out.
The sample
 
public static string TestOut(out string i) 
{ 
i = "out b"; 
return "return value"; 
} 
public static void TestRef(ref string i) 
{ 
// Change the parameters  
i = "ref b"; 
} 
public static void TestNoRef(string refi) 
{ 
//  You don't have to change anything, it's too obvious  
refi = "on c"; 
} 
public Form1() 
{ 
InitializeComponent(); 
} 
private void button1_Click(object sender, EventArgs e) 
{ 
string outi;// No initialization is required  
MessageBox.Show(TestOut(out outi));// The return value  
// The output "return value"; 
MessageBox.Show(outi);// After the call out parameter  
// The output "out b"; 
string refi = "a"; //  Must be initialized  
TestRef(ref refi); //  Call parameters  
MessageBox.Show(refi); 
// The output "ref b"; 
TestNoRef(refi);// Do not use ref 
MessageBox.Show(refi); 
// The output "ref b"; 
} 

Related articles: