Use of ref and out in C #

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace ConsoleApplication8
{
Class Program
{
static void Main (string[] args)
{
int testnum = 100;
Operation (Testnum);
Screen output 100, indicating that the operation of the operation internal to Testnum does not affect the value of Testnum
Console.WriteLine ("Value of Testnum:" +testnum);


                int num1=200;
                otheropearation (ref NUM1);
               //Screen output 10000, which indicates that the operation of otheropearation internal to NUM1 affects the NUM1 value. Same as reference type
                 console.writeline ("Num1 value:" +NUM1);


int num2;
To test the results yourself, the difference between ref and out I think is a need to initialize, one does not need.
Similar to these two methods you can use the ref and out modifier parameters to get the return value you want in case of void return type
Theotheroperation (out num2);
Console.WriteLine ("Value of num2:" +num2);
Console.readkey ();
}

                 static void theotheroperation (out int result)
                {
                Resul   t = 25000;
                 }
                 static void otheropearation (ref int result)
  & nbsp             {
                 result = 1000 0;
                 }
                 static void operation (int result)
    &NBS P           {
                  result = $;
                 }

}
}

Use of ref and out in C #

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.