8.7 Passing Arrays and Array Elements to Methods

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.

Specifying an Array Parameter

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 doubles 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.

Pass-By-Value vs. Pass-By-Reference

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.

Passing an Entire Array vs. Passing a Single Array Element

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.

Fig. 8.13 Passing arrays and individual array elements to methods.

Alternate View

  1   // Fig. 8.13: PassArray.cs
  2   // Passing arrays and individual array elements to methods.
  3   using System;
  4
  5   class PassArray
  6   {
  7      // Main creates array and calls ModifyArray and ModifyElement
  8      static void Main()
  9      {
 10         int[] array = {1, 2, 3, 4, 5};
 11
 12         Console.WriteLine("Effects of passing reference to entire array:");
 13         Console.WriteLine("The values of the original array are:");
 14
 15         // output original array elements
 16         foreach (var value in array)
 17         {
 18            Console.Write($"    {value}");
 19         }
 20
 21         ModifyArray(array); // pass array reference
 22         Console.WriteLine("

The values of the modified array are:");
 23
 24         // output modified array elements
 25         foreach (var value in array)
 26         {
 27            Console.Write($"    {value}");
 28         }
 29
 30         Console.WriteLine("

Effects of passing array element value:
" +
 31            $"array[3] before ModifyElement: {array[3]}");
 32
 33         ModifyElement(array[3]); // attempt to modify array[3]
 34         Console.WriteLine($"array[3] after ModifyElement: {array[3]}");
 35      }
 36
 37      // multiply each element of an array by 2
 38      static void ModifyArray(int[] array2)
 39      {
 40         for (var counter = 0; counter < array2.Length; ++counter)
 41         {
 42            array2[counter] *= 2;
 43         }
 44      }
 45
 46      // multiply argument by 2
 47      static void ModifyElement(int element)
 48      {
 49         element *= 2;
 50         Console.WriteLine($"Value of element in ModifyElement: {element}");
 51      }
 52   }

Effects of passing reference to entire array:
The values of the original array are:
   1   2   3   4   5
The values of the modified array are:
   2   4   6   8   10
Effects of passing array element value:
array[3] before ModifyElement: 8
Value of element in ModifyElement: 16
array[3] after ModifyElement: 8

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).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset