Method parameters ref, OUT problem!

xiaoxiao2021-03-31  185

When the value variable is used as a method parameter, there may be three cases: 1. There is no REF and OUT parameters (direct transmission value, can not change the actual parameters) 2. Use the REF parameter (reference delivery, input, you can change the real parameters) 3. Use OUT parameters (reference delivery, output, you can change the inactive)

4. The difference betweenRef and Out is that one requires initialization, and the other does not need ...

example:

Example (1)

Using system.collections.generic; use system.text;

Namespace TestRef {Class Program {static void testref (ref Int i) {i = 5;} static void test (int i) {i = 5;} static void main (string [] args) {Int i1, i2; i1 = 10; I2 = 10; TestRef (Ref I1); // Test (I2); // console.writeline ("I1 = {0}, I2 = {1}", I1, I2); console.readline (); }}}}: I1 = 15, I2 = 10 Analysis If the method does not use Ref, Test changes the change in the real parameters (i2 = i2 5) to where it is there? Rational explanation is that the pass is a test.

Example (2)

To prove that it is a replication, the design is designed as follows: use system; use system.collections.Generic;

Namespace TestRef {class program {static void testref (Ref INT i) {i = 5;} static void test (int i) {console.writeline ("Part of Test is: {0}", i); i = 5; console.writeline ("Participation in Test is: {0}", i);} static void main (string [] args) {INT I1, I2; I1 = 10; I2 = 10 TestRef (Ref I1); // Test (I2); // console.writeline ("i1 = {0}, I2 = {1}", I1, I2); console.readline ();}}} The arguments in TEST are: 15i1 = 15, i2 = 10

This is fully illustrated, and then transmitting the actual parameters (distinguishing between the function definition, it is called a "form parameter" is called a copy of the collent. This replication is equal to the real parameter, but in memory. In other words, if any of these modifications are separately modified, the impact of this modification will only have an impact on itself and its own copybook is not related to any other intrinsic position!

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

New Post(0)