The role of the Ref,out keyword

Source: Internet
Author: User

Ref is the address of the pass parameter, out is the return value, and the two have some similarities, but there are different points.

You must assign a value to a variable before using ref.

The Out function empties the variable, even if the variable has already been assigned a value, all out-referenced variables are assigned values when exiting the function, and ref references can be modified or unmodified.

The difference can be see the following code:
Static voidOuttest ( out intX out inty) {//before you leave this function, you must assign values to x and Y, otherwise you will get an error. //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 .x =1; Y=2; }        Static voidReftest (ref intXref inty) {x=1; Y=x; }         Public Static voidMain () {//Out Test            intA, B; //variables may not be assigned before out of useOuttest ( outA outb); Console.WriteLine ("A={0};b={1}", A, b); intc = One, d = A; Outtest ( outC outd); Console.WriteLine ("c={0};d ={1}", C, D); //ref Test            intm, N; //reftest (ref m, ref N); //The row above is faulted, and the variable must be assigned before ref is used            into = One, p = A; Reftest (refOrefp); Console.WriteLine ("o={0};p ={1}", O, p); }



The role of the Ref,out keyword

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.