Some understanding of the "ref" and "out" keywords in C #

Source: Internet
Author: User

A Summary (The content of this article is mostly from the network, by my collation, only for the study reference, unavoidably understand the mistake, welcome criticism correct)

    1. In C #, the parameters of a method are passed in four different types:

(1) pass value parameter (by value)

The pass-through parameter does not require additional modifiers. When the value of a parameter is changed during a method call, the parameters of the passed-in method do not change after the method call is complete, but instead retain the value that was originally passed in. Actual

, the pass-through parameter passes a copy of the invocation parameter, so even if the value of the parameter is changed during the call to the method, it does not affect the actual passed-in parameter value, as described in the routine:

(2) address Parameters (by reference)

The address parameter requires a modifier ref(keyword), and if the value of the parameter is changed during the invocation of the method, the parameters of the incoming method are changed after the call is completed. The pass-through parameter passes the memory address of the calling parameter, which points to the same storage location inside and outside the method. Therefore, if the value of the parameter is changed during the invocation of the method, the value of the actual entry and exit parameter will change as well, as described in the following example:

      • The ref method parameter keyword on the method parameter enables the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.
      • To use the ref parameter, the parameter must be passed to the method as a ref parameter display;
      • Parameters passed to the ref parameter must be initialized first;
      • property is not a variable and cannot be passed as a ref parameter;

(3) output parameters (by output)

The output parameter requires a modifierout (keyword).

      • The Out method parameter keyword on the method parameter enables the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.
      • Declaring an out method is useful when you want a method to return more than one value. Methods that use out parameters can still return a value, and a method can have more than one out parameter.
      • Before using the out parameter, you do not have to initialize the variable passed as an out parameter, but you must assign a value to the out parameter before the method returns.
      • property is not a variable and cannot be passed as an out parameter.

(4) array parameters (by array)

The array parameter requires a modifier params.

2. The difference between the REF keyword and the Out keyword

In C #, parameters can be passed either by value or by reference. Passing parameters by reference allows the function member to change the value of the parameter and keep the change, to pass the argument by reference, using the ref and out keywords. Both ref and out can provide similar effects, and they also want to be a pointer variable in C. The difference between them is:

(1) When using a ref parameter, the passed-in parameter must first be initialized. For out, initialization must be done in the method.

(2) When using ref and out, add the ref or out keyword to match the method's parameters and execution method.

(3) Out is suitable for use where multiple return values need to be returned, while ref is used when modifying the caller's reference in a method that needs to be called.

Two Routines

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Collections;6 7 namespaceConsoleApplication58 {9     class ProgramTen     { One         //functions that use the propagation value-passing parameter A         Static voidValuetest (intXinty) -         { -x =3; they =3; -         } -  -         //functions that use the ref address parameter +         Static voidReftest (ref intXref inty) -         { +x =1; Ay =x; at         } -  -         //functions that use out output parameters -         Static voidOuttest ( out intX out inty) -         { -             //before you leave this function, you must assign X and Y values, or you will get an error. in             //y = x; -             //The above line is an error because when out is used, X and y are emptied and need to be re-assigned, even if the function is assigned a value before calling it . tox =1; +y =2; -         } the  *         Static voidMain (string[] args) $         {Panax Notoginseng             /************ Test the normal value of the parameters *******************/ -             intA =4; the             intb =7; +Valuetest (A, b);//Calling this function does not change the value of the variable A, b AConsole.WriteLine ("A={0};b={1}", A, b);//the value of the output, A, a, 4,7 the  +             /************ test "ref" *******************/ -             //int m; $             //int n; $             //reftest (ref m, ref N); -             //the above line is wrong and the variable m,n must be assigned before ref is used. -  the             into = One; -             intp = A;WuyiReftest (refOrefp); theConsole.WriteLine ("M={0};n={1}", O, p);//The values of O and P will change to the value changed in the Reftest function, where the output O is 1,,p to 1 -  Wu             /************ Test the normal value of the parameters *******************/ -             //when using out parameters, you can not initialize the variable c,d first About             intC; $             intD; -Outtest ( outC outD);//The values of C and D will change to the value changed in the Outtest function, where output C is 1,,d to 2 -Console.WriteLine ("c={0};d ={1}", C, D); - console.readline (); A         } +     } the}

Some understanding of the "ref" and "out" keywords 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: 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.