Most popular web site - 256 Arrays Chapter 7 102 // create new

256 Arrays Chapter 7 102 // create new reference and assign it to array 103 array = new int[] { 11, 12, 13 }; 104 } 105 106 // modify elements of array and change reference array 107 // to refer to a new array 108 void SecondDouble( ref int[] array ) 109 { 110 // double each element’s value 111 for ( int i = 0; i < array.Length; i++ ) 112 array[ i ] *= 2; 113 114 // create new reference and assign it to array 115 array = new int[] { 11, 12, 13 }; 116 } 117 } Fig. 7.9 Passing an array reference by value and by reference (Part 3 of 3.). Lines 60 90 perform similar tests, using array variables secondArray and secondArrayCopy and method SecondDouble (lines 108 116). Method Second- Doubleperforms the same operations as FirstDouble, but receives its array argument using keyword ref. In this case, the reference stored in secondArray after the method call is a reference to the array allocated on line 115 of SecondDouble, demonstrating that a reference passed with keyword ref can be modified by the called method so that the reference actually points to a different object, in this case an array allocated in procedure SecondDouble. The if/else structure in lines 85 90 demonstrates that second- Array and secondArrayCopy no longer refer to the same array.
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Leave a Reply