20.2 Motivation for Generic Methods

Overloaded methods are often used to perform similar operations on different types of data. To understand the motivation for generic methods, let’s begin with an example (Fig. 20.1) that contains three overloaded DisplayArray methods (lines 23–31, lines 34–42 and lines 45–53). These methods display the elements of an int array, a double array and a char array, respectively. Soon, we’ll reimplement this program more concisely and elegantly using a single generic method.

Fig. 20.1 Using overloaded methods to display arrays of different types.

Alternate View
  1   // Fig. 20.1: OverloadedMethods.cs
  2   // Using overloaded methods to display arrays of different types.
  3   using System;
  4
  5   class OverloadedMethods
  6   {
  7      static void Main(string[] args)
  8      {
  9         // create arrays of int, double and char
 10         int[] intArray = {1, 2, 3, 4, 5, 6};
 11         double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
 12         char[] charArray = {'H', 'E', 'L', 'L', 'O'};
 13
 14         Console.Write("Array intArray contains: ");
 15         DisplayArray(intArray); // pass an int array argument
 16         Console.Write("Array doubleArray contains: ");
 17         DisplayArray(doubleArray); // pass a double array argument
 18         Console.Write("Array charArray contains: ");
 19         DisplayArray(charArray); // pass a char array argument
 20       }
 21
 22       // output int array
 23       private static void DisplayArray(int[] inputArray)
 24       {
 25          foreach (var element in inputArray)
 26          {
 27             Console.Write($"{element} ");
 28          }
 29
 30          Console.WriteLine();
 31        }
 32
 33       // output double array
 34       private static void DisplayArray(double[] inputArray)
 35       {
 36          foreach (var element in inputArray)
 37          {
 38             Console.Write($"{element} ");
 39          }
 40
 41          Console.WriteLine();
 42       }
 43
 44       // output char array
 45       private static void DisplayArray(char[] inputArray)
 46       {
 47          foreach (var element in inputArray)
 48          {
 49             Console.Write($"{element} ");
 50          }
 51
 52          Console.WriteLine();
 53       }
 54   }
Array intArray contains: 1 2 3 4 5 6
Array doubleArray contains: 1.1 2.2 3.3 4.4 5.5 6.6 7.7
Array charArray contains: H E L L O

The program begins by declaring and initializing three arrays—six-element int array intArray (line 10), seven-element double array doubleArray (line 11) and five-element char array charArray (line 12). Then, lines 14–19 output the arrays.

When the compiler encounters a method call, it attempts to locate a method declaration that has the same method name and parameters that match the argument types in the method call. In this example, each DisplayArray call exactly matches one of the Display-Array method declarations. For example, line 15 calls DisplayArray with intArray as its argument. At compile time, the compiler determines argument intArray’s type (i.e., int[]), attempts to locate a method named DisplayArray that specifies a single int[] parameter (which it finds at lines 23–31) and sets up a call to that method. Similarly, when the compiler encounters the DisplayArray call at line 17, it determines argument double-Array’s type (i.e., double[]), then attempts to locate a method named DisplayArray that specifies a single double[] parameter (which it finds at lines 34–42) and sets up a call to that method. Finally, when the compiler encounters the DisplayArray call at line 19, it determines argument charArray’s type (i.e., char[]), then attempts to locate a method named DisplayArray that specifies a single char[] parameter (which it finds at lines 45–53) and sets up a call to that method.

Study each DisplayArray method. Note that the array element type (int, double or char) appears in one location in each method—the method header (lines 23, 34 and 45). Each foreach statement header (lines 25, 36 and 47) uses var to infer the element type from the method’s parameter. If we were to replace the element types in each method’s header with a generic name (such as T for “type”), then all three methods would look like the one in Fig. 20.2. It appears that if we can replace the array element type in each of the three methods with a single “generic type parameter,” then we should be able to declare one DisplayArray method that can display the elements of any array. The method in Fig. 20.2 will not compile, because its syntax is not correct. We declare a generic DisplayArray method with the proper syntax in Fig. 20.3.

Fig. 20.2 DisplayArray method in which actual type names have been replaced by convention with the generic name T. Again, this code will not compile.

Alternate View
  1   private static void DisplayArray(T[] inputArray)
  2   {
  3      foreach (var element in inputArray)
  4      {
  5         Console.Write($"{element} ");
  6      }
  7
  8      Console.WriteLine();
  9   }
..................Content has been hidden....................

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