C # Use ref and out to pass Arrays

Source: Internet
Author: User

 

Like all out parameters Out The parameter must be assigned a value before it is called.

 

 

Static Void Testmethod1 ( Out Int [] ARR)

{
Arr =New Int[10];//Definite assignment of ARR
}

Like all ref parametersRefParameters must be explicitly assigned by the caller. Therefore, it is not necessary for the receiver to explicitly assign values. You can setRefThe parameter is changed to the result of the call. For example, you can assign a null value to an array or initialize it as another array.

 Static VoidTestmethod2 (Ref Int[] ARR)

{
Arr =New Int[10];//Arr initialized to a different array
}

 

Out example: declare the array thearray In the caller (main method) and initialize the array in the fillarray method. Then, the array elements are returned to the caller and displayed.

Class Testout
{
Static Void Fillarray ( Out Int [] ARR)
{
// Initialize the array:
Arr = New Int [ 5 ] { 1 , 2 , 3 , 4 , 5 };
}

Static VoidMain ()
{
Int[] Thearray;//Initialization is not required

//Pass the array to the callee using out:
Fillarray (OutThearray );

// Display the array elements:
System. Console. writeline ( " Array elements are: " );
For ( Int I = 0 ; I <thearray. length; I ++)
{
System. Console. Write (thearray [I] + "   " );
}

// keep the console window open in debug mode.
system. console. writeline ( " press any key to exit. ");
system. console. readkey ();
}< BR >}< br> /* output:
array elements are:

1 2 3 4 5*/ 

Ref example: Initialize the array thearray In the caller (main method) and useRefThe parameter is passed to the fillarray method. Update some array elements in the fillarray method. Then, the array elements are returned to the caller and displayed.

 

Class Testref
{
Static Void Fillarray ( Ref Int [] ARR)
{
// Create the array on demand:
If (ARR = Null )
{
Arr = New Int [ 10 ];
}
// Fill the array:
Arr [ 0 ] = 1111 ;
Arr [ 4 ] = 5555 ;
}

Static VoidMain ()
{
//Initialize the array:
Int[] Thearray = {1,2,3,4,5};

//Pass the Array Using Ref:
Fillarray (RefThearray );

// Display the updated array:
System. Console. writeline ( " Array elements are: " );
For ( Int I = 0 ; I <thearray. length; I ++)
{
System. Console. Write (thearray [I] + "   " );
}

// keep the console window open in debug mode.
system. console. writeline ( " press any key to exit. ");
system. console. readkey ();
}< BR >}< br> /* output:
array elements are:
1111 2 3 4 5555

*/ 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.