Usage and distinction of ref and out and params usage

Source: Internet
Author: User

Formula: Ref has a in, out only out!

In C #, parameters can be passed either by value or by reference. Passing parameters by reference allows a function member to change the value of a parameter and keep the change. To pass parameters by reference, you can use the ref or out keyword for the corresponding parameter. It works much like a pointer in C.

Usage of ref:

1 namespaceConsoleApplicationTest201601252 {3     class Program4     {5         Static voidMain (string[] args)6         {7             stringA="Initialize! ";8Refmethod (refa);9Console.WriteLine ("usage test for ref keyword! \ t");TenConsole.WriteLine ("results: a="+a); One Console.readkey (); A          } -  -         Private Static voidRefmethod (ref stringa) the         { -A ="executed the ref! "; -         } -        +         -}
View Code

Operation Result:

Usage test for ref keyword!
Results: A= performed the ref!

Use of Out:

1 namespaceConsoleApplicationTest201601252 {3     class Program4     {5         Static voidMain (string[] args)6         {7             stringb//No initialization required8Outmethod ( outb);9Console.WriteLine ("usage test for out keyword! \ t");TenConsole.WriteLine ("results: b="+b); One Console.readkey (); A  -         } -  the         Private Static voidOutmethod ( out stringb) -         { -b ="executed the out!"; -         } +     } -}
View Code

Operation Result:
Usage test for Out keyword
Results: B= performed the out!

Usage and distinction of ref and out and params usage

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.