The role and difference of in,out,ref in C #

Source: Internet
Author: User

In: procedure does not overwrite in contents
Out and out: The value passed in is not read by the procedure, but the procedure can be written
Ref: The value passed in, the procedure reads, and it writes
It's like you're sending the cloth to a tailor's pick-up box (which is the difference between the tailor and the customer)
In: This piece of cloth, can not move, I take the same as the original (I will take this piece of material, is my own business, you do not, but you can not make this piece of material to make any changes, you can only see the texture of this piece of material, color, etc., you want to change this piece of material, that self to the appearance of the material to copy a
Out and out: I may have given you cloth, may not give, also may I give you is just a piece of paper or a piece of sheepskin, but I hope whether I give or not give, you will give me a dress, and put in the bin, as to put the clothes is your thing
Ref: This piece of cloth, guaranteed to be cloth, you can process, but also can not be processed, but whether you are processed or not processed, you have to put me in the recycling bin.
In mode is the default way of passing, that is, the transfer of values inside the function, not explained here


Usually we pass a value to the method. The method obtains a copy of these values and then uses those copies, which are discarded when the method is finished, and the original values are not affected. In addition, we have other methods to pass parameters to the method, reference (ref) and output (out).

Sometimes we need to change the value in the original variable, and we can pass a reference to the variable instead of the value of the variable. A reference is a variable that can access the value of the original variable and modify the reference to modify the value of the original variable. The value of the variable is stored in memory, and you can create a reference He points to the position of the variable in memory. When the reference is modified, the value in memory is modified, so the value of the variable can be modified. When we invoke a method that contains a reference parameter, the arguments in the method point to the corresponding variable that is passed to the method, so we will understand that Why modifying a parameter variable will also result in the value of the original variable.

To create a method to pass a parameter by reference, use the keyword ref.

1 usingSystem;2 classGump3 {4      Public DoubleSquareref Doublex)5     {6x=x*x;7         returnx;8     }9 }Ten  One classTestApp A { -      Public Static voidMain () -     { theGump doit=NewGump (); -          -         DoubleA=3; -         Doubleb=0; +          -Console.WriteLine (\"before Square->a={0},b={1}\ ", A, b); +              AB=doit.square (refa); atConsole.WriteLine (\"After square->a={0},b={1}\ ", A, b); -     } -}

Passing the test, we found that the value of a has been modified to 9.


By specifying the return type, you can return a value from the method, sometimes (perhaps not, but we should have this method) and need to return multiple values, although we can use ref to do it, but C # specifically provides an attribute type with the keyword out. After the introduction, We will explain the difference between ref and out.

By using the Out keyword, we changed the value of three variables, which means that out is the outgoing value from the method.

1 usingSystem;2 classGump3 {4      Public voidMath_routines (DoubleX out DoubleHalf, out DoubleSquared, out DoubleCubed)5 //can be: public void Math_routines (ref double x,out double half,out double squared,out double cubed)6 //However, this is not possible: public void Math_routines (out double x,out double half,out double squared,7 //Out Double cubed), for this example, because the value of the output is to be assigned by x, so x can no longer be the output value8     {9half=x/2;Tensquared=x*x; Onecubed=x*x*x; A     } - } -  the classTestApp - { -      Public Static voidMain () -     { +Gump doit=NewGump (); -          +         Doublex1= -; A         Doublehalf1=0; at         Doublesquared1=0; -         Doublecubed1=0; - [Page] -         /* - double x1=600; - double half1; in double squared1; - double cubed1; to         */ +          -Console.WriteLine (\"before method->x1={0}\ ", X1); theConsole.WriteLine (\"half1={0}\ ", half1); Console.WriteLine (\ "Squared1={0}\", squared1); *Console.WriteLine (\"cubed1={0}\ ", cubed1); $         Panax Notoginseng          -          theDoit.math_rountines (x1, outHalf1, outSquared1, outcubed1); +          AConsole.WriteLine (\"After method->x1={0}\ ", X1); theConsole.WriteLine (\"half1={0}\ ", half1); +Console.WriteLine (\"squared1={0}\ ", squared1); -Console.WriteLine (\"cubed1={0}\ ", cubed1); $     } $}

We found that ref and out seem to be able to achieve the same functionality. Because you can change the value of a variable passed to a method. However, the essential difference between the two is that ref is an incoming value, and out is an outgoing value. In a method that contains an out keyword, the variable must be free from the method parameter, which can be a ref ) variable assignment or by a global (that is, the method can be used by the external variable) variable assignment, the purpose of out is to ensure that each outgoing variable must be assigned value.

The above code is/**/commented out section, can be used directly. That is, you can not initialize the variable before calling the method. But the "x1\" is to be assigned, otherwise an error is given. The ref parameter, when passed to the method, is already a value, so ref focuses on the output.

The role and difference of in,out,ref in C #

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: 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.