REF and OUT

xiaoxiao2021-03-06  13

The REF keyword is like the &, the role is the declaration, indicating that this parameter is a pass value called, that is, not copying the argument to the function running in a function, and the final function exit will pace parameter destructor . Instead, the address of the city parameters is transmitted, and the operations in the function are the parameters itself. The following example can be explained.

Using system;

Namespace consoleapplication4 {class class1 {private static int a = 3; private static int b = 5; static void main (string [] args) {OUTPUT (); swap (ref A, ref b); output ();}

Private static void swap (Ref Int A, REF INT B) {INT TEMP = B; B = a; a = TEMP;

Private static void output () {console.writeLine ("a = {0}; b = {1}", A, b);}

}

result

A = 3; b = 5

A = 5; b = 3

If you don't need a Ref keyword

A = 3; b = 5

A = 3; b = 5

-------------------------------------------------- -------------------------------------------------- ---------------------------------------------

The OUT keyword and the REF keyword are very similar, but there are two points, the following is given, then the difference is different.

9CBS example:

Using system; public class myclass {public static int testout (out char i) {i = 'b'; return -1;} public static void main () {char i; // variable need not be initialized console.writeline (Testout (OUT I)); console.writeline (i);}}

My example:

Using system;

Namespace consoleapplication4 {class class1 {private static int a; private static int b; static void main (string [] args) {// output (); swap (out a, out b); output ();}

Private static void swap (out int a, out amount = b; b = 5; a = 3;}

Private static void output () {console.writeLine ("a = {0}; b = {1}", A, b);}

}

Two points

Popular saying is that there is a value outside the REF function. OUT must have not been assigned, then assign a value in the function body and then use it.

OUT must assign a value to the parameter in the function body

A Variable Passed As an out Argument NEED NOT BE INTILALIZED. HOWEVER, The OUT Parameter Must Be Assigned a value before the method Returns. The property cannot be passed as an OUT parameter.

A Property is not a variable and cannot be passed as an out parameter.

What to pay attention to is

An overload will occur if declarations of two methods differ only in their use of out However, it is not possible to define an overload that only differs by ref and out For example, the following overload declarations are valid..:

Two functions cannot be overloaded because of REF and OUT.

Let's talk about passing array parameters

EXAMPLE 1

In this Example, THE ARRAY MYARRAY IS DECLARED IN The Caller (The Main Method), AND Initialized in The Fillarray Method. The Array Elements Are Returned to The Caller and Displayed.

// cs_array_ref_and_out.cs

Using system;

Class Testout

{

Static Public Void Fillarray (Out Int [] MyArray)

{

// Initialize the array:

MyArray = new int =NT [5] {1, 2, 3, 4, 5};

}

Static public void main ()

{

int [] MyArray; // Initialization is Not Required

// pass the array to the callee using out:

Fillarray (Out myarray);

// Display the array elements:

Console.writeline ("Array Elements Are:");

For (int i = 0; i

Console.writeline (MyArray [i]);

}

}

OUTPUT

Array Elements Are:

1

2

3

4

5

OUT is only declared but is not assigned, and then the value is ignited.

EXAMPLE 2

In this example, the array myArray is initialized in the caller (the Main method), and passed to the FillArray method by using the ref parameter. Some of the array elements are updated in the FillArray method. Then, the array elements are returned to The Caller and Displayed.

// cs_array_ref_and_out2.cs

Using system;

Class testRef

{

Public Static Void Fillarray (Ref Int [] Arr)

{

// Create The Array On Demand:

IF (arr == null)

Arr = new int [10];

// OtherWise Fill The Array:

Arr [0] = 123;

Arr [4] = 1024;

}

Static public void main ()

{

// Initialize the array:

Int [] myarray = {1, 2, 3, 4, 5};

// pass the array using ref:

Fillarray (Ref myarray);

// Display the Updated Array:

Console.writeline ("Array Elements Are:");

For (int i = 0; i

Console.writeline (MyArray [i]);

}

}

OUTPUT

Array Elements Are:

123

2

3

4

1024

REF can be assigned to values, or it may not be assigned, and the sum of the information is the address.

转载请注明原文地址:https://www.9cbs.com/read-49997.html

New Post(0)