Use ref and out in C!

Source: Internet
Author: User
Ref
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.
Create a method for passing parameters by reference. Use the keyword ref. For example;
Using system;
Class Gump
{
Public double square (ref Double X)
{
X = x * X;
Return X;
}
}

Class testapp
{
Public static void main ()
{
Gump doit = new Gump ();

Double A = 3;
Double B = 0;

Console. writeline ("before square-> A = {0}, B = {1}", a, B );

B = doit. Square (Ref );
Console. writeline ("after square-> A = {0}, B = {1}", a, B );
}
}

Through the test, we found that the value of a has been changed to 9.

Out
By specifying the return type, you can return a value from the method. Sometimes (maybe we haven't met it yet, but we should have such a method), you need to return multiple values, although we can use ref, C # provides an attribute type with the keyword "out. after the introduction, we will explain the differences between ref and out.
Using system;
Class Gump
{
Public void math_routines (Double X, out double half, out double squared, out double cubed)
// It Can Be: Public void math_routines (// ref Double X, out double half, out double squared, out double cubed)
// However, this is not allowed: Public void math_routines (out double X, out double half, out double squared, out double cubed). In this example, because the output value depends on X, X cannot be used for the output value.
{
Half = X/2;
Squared = x * X;
Cubed = x * X;
}
}

Class testapp
{
Public static void main ()
{
Gump doit = new Gump ();

Double X1 = 600;
Double half1 = 0;
Double squared1 = 0;
Double cubed1 = 0;

/*
Double X1 = 600;
Double half1;
Double squared1;
Double cubed1;
*/

Console. writeline ("before method-> X1 = {0}", X1 );
Console. writeline ("half1 = {0}", half1 );
Console. writeline ("squared1 = {0}", squared1 );
Console. writeline ("cubed1 = {0}", cubed1 );

Doit. math_rountines (x1, out half1, out squared1, out cubed1 );

Console. writeline ("after method-> X1 = {0}", X1 );
Console. writeline ("half1 = {0}", half1 );
Console. writeline ("squared1 = {0}", squared1 );
Console. writeline ("cubed1 = {0}", cubed1 );
}
}

By using the out keyword, we change the values of the three variables, that is, the out is the value transmitted from the method.

We found that the ref and out functions seem to be the same. because the value of the variable passed to the method can be changed. however, the essential difference between the two is that ref is the incoming value, and out is the outgoing value. in a method that contains the out keyword, the variable must be assigned a value by a variable that does not include out (which can be ref) in the method parameter or by a global variable (that is, the external variable of the method that can be used by the method) variable assignment. The out principle is to ensure that every outgoing variable must be assigned a value.
You can directly use the/***/commented out part of the code above. that is to say, you can not initialize the variable before calling the method. however, "X1" requires a value. Otherwise, an error is returned. the ref parameter has a value when it is passed to the method, so the ref parameter focuses on modification. out focuses on output.

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.