The connection and difference between c#:ref and out.

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 whether it is a function definition or a call. You can compare the code: code ①:
1 class Program 2     {3         static void Main (string[] args) 4         {5 program             PG = new program (); 6             int x = 10; 7             int y = 8             pg. GetValue (ref x, ref  y); 9             Console.WriteLine ("X={0},y={1}", X, y);             console.readline ();           13         }14 public         void GetValue (ref int x, ref int y)-         {             x = 521;18             y = 520;19         }20     }

Run the result as

Code ②:

1 class Program 2     {3         static void Main (string[] args) 4         {5 program             PG = new program (); 6             int x = 10; 7             int y = 8             pg. GetValue (ref x, ref  y); 9             Console.WriteLine ("X={0},y={1}", X, y);             console.readline ();           13         }14 public         void GetValue (ref int x, ref int y)-         {             x = 1000;18             y = 1;19         }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:
1  Class Program 2     {3         static void Main (string[] args) 4         {5 program             PG = new program (); 6             int x; 7             int y;        If x, Y is not initialized here, the compilation does not pass. 8             pg. GetValue (ref x, ref  y); 9             Console.WriteLine ("X={0},y={1}", X, y);             console.readline ();           13         }14 public         void GetValue (ref int x, ref int y)-         {             x = 1000;18             y = 1;19         }    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 ④:
1 class Program 2     {3         static void Main (string[] args) 4         {5 program             PG = new program (); 6             int x=10; 7< C5/>int y=233; 8             pg. Swap (out x, out y); 9             Console.WriteLine ("X={0},y={1}", X, y);             console.readline ();           }14 public         void Swap (out int a,out  int b)-         {+             -             int temp = A;   A, B does not have an initial value assigned inside the function, then an error occurs.             a = b;20             b = temp;21         }     

An error occurred: The unassigned out parameter "a" is used, and "B" is correctly run when the SWAP function is defined to assign the initial value to A/b. To summarize the above four, the difference between the use of ref and Out is: ①:REF The specified parameter must be initialized at the time of the function call and cannot be an empty reference. The parameters specified in the out can be uninitialized at the time of the function call, and the parameters specified by ②:out will be emptied when entering the function, and the initial value must be assigned within the function. The parameters specified by ref are not required.

The connection and difference between c#:ref and out.

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.