Simple differences between C # ref and out

Source: Internet
Author: User

1 class Program2     {3        //ref and out like C + + pointers, of course, can not be used directly when the pointer, is able to reference some parameters, but not directly as an address to control parameters (approximate meaning)4         Static voidMain (string[] args)5         {6             intA, B;7Outtest ( outA outb);8Console.WriteLine ("A={0},b={1}", A, b);9             //output A = 1;b = 2;Ten  One             intc = One, d = A; AOuttest ( outC outd); -Console.WriteLine ("A={0},b={1}", A, b); -             //output c = 1;d = 2; the  -             intm = One, n = A; -             //reftest (ref m, ref N); -Reftest (refMrefn); +Console.WriteLine ("M={0},n={1}", M, n); -             //output m = 22;n = one; +  A Swap (M, n); atConsole.WriteLine ("M={0},n={1}", M, n); -             //output m = 22;n = one; -         } -  -         Static voidOuttest ( out intX out inty) -         { in             //method can be used outside the body without assigning a value to the outgoing parameter -             //The method body, must assign the value to the outgoing parameter, otherwise error, for example: x=y; to             //functions that apply to multiple return values +x =1; -y =2; the         } *  $         Static voidReftest (ref intXref inty)Panax Notoginseng         { -  the             //method Body, you must assign a value to the parameter, otherwise the error +             //within the method body, ref can assign or not assign a value to a parameter A             //suitable for referencing the original value and modifying the original value the             inttemp =x; +x =y; -y =temp; $         } $  -         Static voidSwap (intAintb) -         { the             inttemp =A; -A =b;Wuyib =temp; the         } -}

Simple differences between C # ref and out

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.