asp.net (C #) Ref,out, params difference _ practical skills

Source: Internet
Author: User
No.1 Params
A keyword that allows a method (function) to have a variable parameter.
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, no longer described below)
Copy Code code 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 Button1_Click (object sender, EventArgs e)
{
Useparams (1, 2, 3);/See parameter is 3
Useparams (1, 2); See the parameters are 2, variable bar
USEPARAMS2 (1, ' A ', "test");
int[] myarray = new Int[3] {10, 11, 12};
Useparams (myarray); See also can be container class, variable bar:)
}
}

No.2 out
This is a reference pass L.
Principle one: When a method (function) uses out as an argument, any changes made to the out parameter in the method (function) are reflected in the variable.
Principle two: Declaring an out method is useful when you want a method to return multiple values. Methods that use out parameters can still return a value. A method can have more than one out parameter.
Principle three: 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.
Principle four: You do not have to initialize a variable that is passed as an out parameter. Because the out parameter empties itself after entering the method (function), makes itself a clean parameter, and for this reason it must assign a value to the out parameter before the method returns (only parameters with an address that have no value cannot be accepted by. NET).
Principle five: A property is not a variable and cannot be passed as an out parameter.
Principle Six: Overloading occurs if the declarations of two methods differ only in the use of out. However, you cannot define overloads that differ only in the ref and out aspects. For example, the following overload declaration is valid:
Copy Code code as follows:

Class MyClass
{
public void MyMethod (int i) {i = 10;}
public void MyMethod (out int i) {i = 10;}
}

The following overload declaration is not valid:
Copy Code code as follows:

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 with ref and out.
Example attached
No.2 ref
Ref is just an address!!!
Principle one: When a method (function) uses ref as a parameter, any changes made to the ref parameter in the method (function) are reflected in the variable.
Principle two: When you invoke a method, any changes you make to the parameter in the method are reflected in the variable.
Principle three: To use the ref parameter, you must explicitly pass the parameter to the method as a ref parameter. The value of the ref parameter can be passed to the ref parameter.
Principle four: The variable passed by the ref parameter must be initialized, because the ref parameter is either after the method (the function) or itself, and the address is pointing to the original value, and because of this reason the ref parameter can also not operate inside the method in which it is used.
Principle Six: If the declarations of both methods differ only in their use of ref, an overload will occur. However, you cannot define overloads that differ only in the ref and out aspects. For example, the following overload declaration is valid:
Copy Code code as follows:

Class MyClass
{
public void MyMethod (int i) {i = 10;}
public void MyMethod (ref int i) {i = 10;}
}

However, the following overload declaration is invalid:
Copy Code code as follows:

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 with ref and out.
Example
Copy Code code as follows:

public static string Testout (out string i)
{
i = ' out B ';
Return ' return value ';
}
public static void Testref (ref string i)
{
//change parameter
i = "ref B";
}
public static void Testnoref (String refi)
{
//Do not change anything, this is too obvious
Refi = "on C";
}
Public Form1 ()
{
InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)
{
String outi;//do not need to initialize
Messagebox.sho W (Testout (out Outi));//Returns the value
//output "return value";
MessageBox.Show (Outi);//Out parameters after call
//output "out B";
String refi = "a";//Must initialize
Testref (ref refi);//Call parameter
MessageBox.Show (refi);
//output "ref B";
Testnoref (refi);//Do not use ref
MessageBox.Show (refi);
//output "ref B";
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.