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
*/