To pass an array argument to a method, specify the name of the array without any brackets. For example, if hourlyTemperatures
is declared as
var hourlyTemperatures = new double[24];
then the method call
ModifyArray(hourlyTemperatures);
passes the reference of array hourlyTemperatures
to method ModifyArray
. Every array object “knows” its own length (and makes it available via its Length
property). Thus, when we pass an array object’s reference to a method, we need not pass the array length as an additional argument.
For a method to receive an array reference through a method call, the method’s parameter list must specify an array parameter. For example, the method header for method Modify-Array
might be written as
void ModifyArray(double[] b)
indicating that ModifyArray
receives the reference of an array of double
s in parameter b
. The method call passes array hourlyTemperatures
’ reference, so when the called method uses the array variable b
, it refers to the same array object as hourlyTemperatures
in the calling method.
When an argument is an entire array or an individual array element of a reference type, the called method receives a copy of the reference. However, when an argument to a method is an individual array element of a value type, the called method receives a copy of the element’s value. To pass an individual array element to a method, use the indexed name of the array as an argument (e.g., hourlyTemperatures[2]
). If you want to pass a value-type array element to a method by reference, you must use the ref
keyword as shown in Section 7.18.
Figure 8.13 demonstrates the difference between passing an entire array and passing a value-type array element to a method. The foreach
statement at lines 16–19 outputs the five int
elements of array
. Line 21 invokes method ModifyArray
, passing array
as an argument. Method ModifyArray
(lines 38–44) receives a copy of array
’s reference and uses the reference to multiply each of array
’s elements by 2
. To prove that array
’s elements (in Main
) were modified, the foreach
statement at lines 25–28 outputs the five elements of array
again. As the output shows, method ModifyArray
doubled the value of each element.
Figure 8.13 next demonstrates that when a copy of an individual value-type array element is passed to a method, modifying the copy in the called method does not affect the original value of that element in the calling method’s array. To show the value of array[3]
before invoking method ModifyElement
, lines 30–31 display the value of array[3]
, which is 8
. Line 33 calls method ModifyElement
(lines 47–51) and passes array[3]
as an argument. Remember that array[3]
is actually one int
value (8
) in array
. Therefore, the app passes a copy of the value of array[3]
. Method ModifyElement
multiplies the value received as an argument by 2, stores the result in its parameter element
, then outputs the value of element
(16
). Since method parameters, like local variables, cease to exist when the method in which they’re declared completes execution, the method parameter element
is destroyed when method ModifyElement
terminates. When the app returns control to Main
, line 34 displays the unmodified value of array[3]
(i.e., 8
).