The difference between c#ref and out-ref is that there is a go-out that out is only out of the

Source: Internet
Author: User

I had this problem when I was learning C #, but I didn't dig into it. Last night, when I thought about this problem, I tried to knock the code, and the result from the running, the more you see the more chaotic. On the basis of the review of some information, I summed up a bit. It might be a little messy, but it's something that I've summed up. One: The REF keyword enables parameters to be passed by reference. The effect is that 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, both the method definition and the calling method must explicitly use the REF keyword. in other words, setting and changing parameters in the method will directly affect the function call (code ① and ②). The keyword ref cannot be ignored either in the definition of a function or when it is called.You can compare the code:Code ①:
1ClassProgram2{3Staticvoid Main (String[] args)4{5 Program PG =NewProgram ();6int x =10;7int y =20;8 pg. GetValue (Ref x,Refy);9 Console.WriteLine ("X={0},y={1}", x, y);1011  Console.ReadLine (); 12 13 }14 15 public void GetValue (ref int x, ref int y) 16  {17 x = 521;18 y = 520;}20}         

run the result as

Code ②:

1ClassProgram2{3Staticvoid Main (String[] args)4{5 Program PG =NewProgram ();6int x =10;7int y =20;8 pg. GetValue (Ref x,Refy);9 Console.WriteLine ("X={0},y={1}", x, y);1011 Console.ReadLine (); 12 13 }14 15 public void GetValue (ref int x, ref int y) 16  {17 x = 1000;18 y = 1;}20}        

By the running results of code ① and ②, any changes to the parameters in the method will be reflected in the variable, and the assignment of the parameters in the main function does not work, does it mean that no initialization is required? To see the 2nd

Two: The parameters defined by ref must be initialized before use, regardless of whether the parameter is given an initial value at the time of the function definition. This is exactly the same as the argument that the out specified can not be initialized when the function is called. look at the code ③ and its running results:
1ClassProgram2{3Staticvoid Main (String[] args)4{5 Program PG =NewProgram ();6IntX7int y;//If x, Y is not initialized here, the compilation does not pass.8 pg. GetValue (Ref x,Refy);9 Console.WriteLine ("X={0},y={1}", x, y);1011 Console.ReadLine (); 12 13 }14 15 public void GetValue (ref int x, ref int y) 16  {17 x = 1000;18 y = 1; 20}         

The error that occurs is that the unassigned local variable "x", "Y" is used. It can be explained that the parameter specified by ref has no initial value given when the function is defined, and must be initialized when used.

Third: For out, the first article also applies. If you modify the code ① and ref in ② all to out, you can get the same result as using Ref.  Four: Out the specified parameter must be assigned an initial value at the time the function is defined. Otherwise, an error occurs. The parameter specified by ref can not be assigned the initial value inside the function, and the initial value can be assigned when the function is called. Code ④:
1ClassProgram2{3Staticvoid Main (String[] args)4{5 Program PG =NewProgram ();6int x=10;7int y=233;8 pg. Swap (Out X,Outy);9 Console.WriteLine ("X={0},y={1}", x, y);1011Console.ReadLine ();1213}1415 public void Swap ( Span style= "color: #0000ff;" >out int a,out int b) 16  {17 18 int temp = A; //a,b has no initial value assigned inside the function, an error occurs. 19 a = B; b = Temp;  22 23}   

An error occurred: An unassigned out parameter "a" was used, "B"when the swap function is defined, assigning the initial value to a and B is correct. to summarize the above four, the difference between using ref and Out is:①:REF The specified parameter must be initialized at the time of the function call and cannot be a null reference. The parameters specified in the out can be uninitialized when the function is called;②:out The specified parameter empties itself into the function and must be assigned an initial value inside the function. The parameters specified by ref are not required.

The difference between c#ref and out-ref is that there is a go-out that out is only out of the

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.