Ref reference type, array parameter, out output parameter

Source: Internet
Author: User

Similarities and differences between ref and out

Commonalities: All references are passed.
Difference: the ref parameter must be assigned a value before being called. You can leave no value during method calling.
The out parameter can be assigned no value before being called. It must be assigned a value during the method call process.

 

// Method parameters
Class Program
{
Static void main (string [] ARGs)
{
// Value transfer
// Int num = 5;
// Change (Num );
// Console. writeline ("Num in the main method is:" + num );

// Reference Transmission
// Int [] arr = {1, 3, 5 };
// Change (ARR );
// Console. writeline ("arr [1] In the main method is:" + arr [1]);

// Ref parameter (reference transfer)
// Int num = 5;
// Change (ref num); // The ref parameter must be assigned a value before calling.
// The num in the console. writeline ("Main (REF) method is:" + num );

// Out parameter (reference transfer)
// Int num;
// Outchange (Out num );
// The num in the console. writeline ("Main (out) method is:" + num );

// Array parameters (transfer by reference)
Int result = sum ("*", 3, 5, 6 );
Console. writeline (result );
}

Public static void change (INT number)
{
Number = 10;
Console. writeline ("number in change is:" + number );
}

Public static void change (INT [] ARR)
{
Arr [1] = 100;
Console. writeline ("arr [1] In change is:" + arr [1]);
}

Public static void change (ref int number)
{
Number = 10;
Number in console. writeline ("Change (ref parameter) is:" + number );
}

Public static void outchange (Out int number)
{
Number = 20;
Console. writeline ("outchange (out parameter) in number:" + number );
}

// Note: array parameters must be placed at the end of the parameter list
Public static int sum (string op, Params int [] num)
{
If (OP = "+ ")
{
Int Total = 0;
For (INT I = 0; I <num. length; I ++)
{
Total + = num [I];
}
Return total;
}
Else if (OP = "*")
{
Int Total = 1;
For (INT I = 0; I <num. length; I ++)
{
Total * = num [I];
}
Return total;
}
Else
{
Console. writeline ("the operator can only be + /*");
Return-1;
}

}
}

 

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.