Convert ref and out to ref and out

Source: Internet
Author: User
Ref and out

Today, my younger brother asked me about the difference between ref and out.
In C #, ref and out are two special keywords. You can use them to pass parameters according to references.
In general, we usually pass the value to the method. the method obtains a copy of these values, and then uses these copies. After the method is run, these copies will be discarded, and the original values will not be affected. in addition, we have other parameters passed to the method, including reference (REF) and output (out ).
Sometimes, we need to change the value of the original variable. In this case, we can pass the reference of the variable to the method, instead of the value of the variable. A reference is a variable that can access the value of the original variable. modifying a reference changes the value of the original variable. the value of a variable is stored in the memory. You can create a reference pointing to the position of the variable in the memory. when the reference is modified, the value in memory is modified, so the value of the variable can be modified. when we call a method that contains referenced parameters, the parameters in the method point to the corresponding variable passed to the method. Therefore, we will understand that, why does the modification of the Parameter Variable also lead to the value of the original variable.
In terms of functionality, using these two keywords allows a method to return multiple parameters.
Definition in msdn:
The ref keyword allows the parameter to be passed by reference. The effect is that when the control is passed back to the call method, any changes made to the parameters in the method will be reflected in the variable. To use the ref parameter, you must explicitly use the ref keyword for both method definition and call methods.
The out keyword causes the parameter to be passed through reference. This is similar to the ref keyword, except that the ref requires that the variables must be initialized before being passed. To use the out parameter, the out keyword must be explicitly used for method definition and call.
 

First, let's look at a simple example:

Static void testrefandout ()
{
String S1 = "good luck! ";
Testref (ref S1 );
Console. writeline (S1); // output: Hello world!
}
Static void testref (ref string Str)
{
STR = "Hello world! ";
}
In testrefandout (), pass the string S1 to the method testref (ref string Str) using the ref keyword. In this method, we change the value of the reference variable str of S1. Finally, return to the testrefandout () method and output the value of S1. It is found that the value has been changed.

Replace ref in the previous example with out,CodeAs follows:

Static void testrefandout ()
{
String S1 = "good luck! ";
// Testref (ref S1 );
Testout (Out S1 );
Console. writeline (S1); // output: Hello world!
}

Static void testout (out string Str)
{
STR = "Hello world! ";
}
Similarly, after changing from ref to out, the final output is still the same. What are the differences between the two keywords?

Further test:

Ref:

Static void testrefandout ()
{
String S1 = "good luck! ";
Testref (ref S1 );
}

Static void testref (ref string Str)
{
Console. writeline (STR); // output: good lick!
}
Out

Static void testrefandout ()
{
String S1 = "good luck! ";
Testout (Out S1 );
}

Static void testout (out string Str)
{
Console. writeline (STR); // compile does not pass
}
The Code of ref is compiled smoothly, and "Good luck!" Is output! ", But the out Code cannot be compiled, and the prompt" use of unassigned out parameter 'str' "is displayed, that is, the out parameter STR with an unallocated address is used. What's going on?

When the original out parameter enters the method, C # automatically clears all its references and directions. Therefore, in the preceding out example, you must assign a value to the STR parameter first. See the followingProgram.

Static void testrefandout ()
{
String S1 = "good luck! ";
Testout (Out S1 );
}
Static void testout (out string Str)
{
STR = "Hello world! ";
Console. writeline (STR); // output: Hello world!
}
OK. The first difference is that when the out parameter enters the method (function), it clears itself and turns itself into a clean parameter, for this reason, you must assign a value to the out parameter before the method is returned or before the out parameter is used.. Net), while the ref parameter does not need to be assigned before being used by the called method, or even the value of the ref parameter is not changed in the called method, this will not cause compilation errors.

Continue to read a piece of code:

Ref:

Static void testrefandout ()
{
String S1;
Testref (ref S1 );
Console. writeline (S1); // compile does not pass!
}
Static void testref (ref string Str)
{
STR = Hello world! ";
}
Out:
Static void testrefandout ()
{
String S1;
Testout (Out S1 );
Console. writeline (S1); // output: Hello world!
}

static void testout (out string Str)
{< br> STR = "Hello world! ";
}< br> this time, we found that the ref code cannot be compiled, and S1 is an empty reference, so it cannot be used. The out parameter does not care whether S1 is a null reference because it turns S1 into a null reference even if S1 is not a null reference. OK. The second difference is that the ref parameter must be initialized before use, but not out. Well, the above two differences can be extended. The out parameter can only be input and the ref parameter can be input and output. The usage is summarized as follows: Out is suitable for use where multiple retrun return values are required, while ref is used when the method to be called modifies the reference of the caller.

Today, my younger brother asked me about the difference between ref and out.
In C #, ref and out are two special keywords. You can use them to pass parameters according to references.
In general, we usually pass the value to the method. the method obtains a copy of these values, and then uses these copies. After the method is run, these copies will be discarded, and the original values will not be affected. in addition, we have other parameters passed to the method, including reference (REF) and output (out ).
Sometimes, we need to change the value of the original variable. In this case, we can pass the reference of the variable to the method, instead of the value of the variable. A reference is a variable that can access the value of the original variable. modifying a reference changes the value of the original variable. the value of a variable is stored in the memory. You can create a reference pointing to the position of the variable in the memory. when the reference is modified, the value in memory is modified, so the value of the variable can be modified. when we call a method that contains referenced parameters, the parameters in the method point to the corresponding variable passed to the method. Therefore, we will understand that, why does the modification of the Parameter Variable also lead to the value of the original variable.
In terms of functionality, using these two keywords allows a method to return multiple parameters.
Definition in msdn:
The ref keyword allows the parameter to be passed by reference. The effect is that when the control is passed back to the call method, any changes made to the parameters in the method will be reflected in the variable. To use the ref parameter, you must explicitly use the ref keyword for both method definition and call methods.
The out keyword causes the parameter to be passed through reference. This is similar to the ref keyword, except that the ref requires that the variables must be initialized before being passed. To use the out parameter, the out keyword must be explicitly used for method definition and call.
 

First, let's look at a simple example:

Static void testrefandout ()
{
String S1 = "good luck! ";
Testref (ref S1 );
Console. writeline (S1); // output: Hello world!
}
Static void testref (ref string Str)
{
STR = "Hello world! ";
}
In testrefandout (), pass the string S1 to the method testref (ref string Str) using the ref keyword. In this method, we change the value of the reference variable str of S1. Finally, return to the testrefandout () method and output the value of S1. It is found that the value has been changed.

Replace ref with out in the previous example. The Code is as follows:

Static void testrefandout ()
{
String S1 = "good luck! ";
// Testref (ref S1 );
Testout (Out S1 );
Console. writeline (S1); // output: Hello world!
}

Static void testout (out string Str)
{
STR = "Hello world! ";
}
Similarly, after changing from ref to out, the final output is still the same. What are the differences between the two keywords?

Further test:

Ref:

Static void testrefandout ()
{
String S1 = "good luck! ";
Testref (ref S1 );
}

Static void testref (ref string Str)
{
Console. writeline (STR); // output: good lick!
}
Out

Static void testrefandout ()
{
String S1 = "good luck! ";
Testout (Out S1 );
}

Static void testout (out string Str)
{
Console. writeline (STR); // compile does not pass
}
The Code of ref is compiled smoothly, and "Good luck!" Is output! ", But the out Code cannot be compiled, and the prompt" use of unassigned out parameter 'str' "is displayed, that is, the out parameter STR with an unallocated address is used. What's going on?

When the original out parameter enters the method, C # automatically clears all its references and directions. Therefore, in the preceding out example, you must assign a value to the STR parameter first. See the following procedure.

Static void testrefandout ()
{
String S1 = "good luck! ";
Testout (Out S1 );
}
Static void testout (out string Str)
{
STR = "Hello world! ";
Console. writeline (STR); // output: Hello world!
}
OK. The first difference is that when the out parameter enters the method (function), it clears itself and turns itself into a clean parameter, for this reason, you must assign a value to the out parameter before the method is returned or before the out parameter is used.. Net), while the ref parameter does not need to be assigned before being used by the called method, or even the value of the ref parameter is not changed in the called method, this will not cause compilation errors.

Continue to read a piece of code:

Ref:

Static void testrefandout ()
{
String S1;
Testref (ref S1 );
Console. writeline (S1); // compile does not pass!
}
Static void testref (ref string Str)
{
STR = Hello world! ";
}
Out:
Static void testrefandout ()
{
String S1;
Testout (Out S1 );
Console. writeline (S1); // output: Hello world!
}

static void testout (out string Str)
{< br> STR = "Hello world! ";
}< br> this time, we found that the ref code cannot be compiled, and S1 is an empty reference, so it cannot be used. The out parameter does not care whether S1 is a null reference because it turns S1 into a null reference even if S1 is not a null reference. OK. The second difference is that the ref parameter must be initialized before use, but not out. Well, the above two differences can be extended. The out parameter can only be input and the ref parameter can be input and output. The usage is summarized as follows: Out is suitable for use where multiple retrun return values are required, while ref is used when the method to be called modifies the reference of the caller.

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.