Overloaded methods are often used to perform similar operations on different types of data. To motivate generic methods, let's begin with an example (Fig. 26.1) that contains three overloaded PrintArray methods (lines 2329, lines 3238 and lines 4147). These methods display the elements of an int array, a double array and a char array, respectively. Soon, we will reimplement this program more concisely and elegantly using a single generic method.
Figure 26.1. Displaying arrays of different types using overloaded methods.
1 // Fig. 26.1: OverloadedMethods.cs 2 // Using overloaded methods to print 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.WriteLine( "Array intArray contains:" ); 15 PrintArray( intArray ); // pass an int array argument 16 Console.WriteLine( "Array doubleArray contains:" ); 17 PrintArray( doubleArray ); // pass a double array argument 18 Console.WriteLine( "Array charArray contains:" ); 19 PrintArray( charArray ); // pass a char array argument 20 } // end Main 21 22 // output int array 23 static void PrintArray( int[] inputArray ) 24 { 25 foreach ( int element in inputArray ) 26 Console.Write( element + " " ); 27 28 Console.WriteLine( " " ); 29 } // end method PrintArray 30 31 // output double array 32 static void PrintArray( double[] inputArray ) 33 { 34 foreach ( double element in inputArray ) 35 Console.Write( element + " " ); 3637 Console.WriteLine( " " ); 38 } // end method PrintArray 39 40 // output char array 41 static void PrintArray( char[] inputArray ) 42 { 43 foreach ( char element in inputArray ) 44 Console.Write( element + " " ); 45 46 Console.WriteLine( " " ); 47 } // end method PrintArray 48 } // end class OverloadedMethods
|
The program begins by declaring and initializing three arrayssix-element int array intArray (line 10), seven-element double array doubleArray (line 11) and five-element char array charArray (line 12). Then, lines 1419 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 PrintArray call exactly matches one of the PrintArray method declarations. For example, line 15 calls PrintArray with intArray as its argument. At compile time, the compiler determines argument intArray's type (i.e., int[]), attempts to locate a method named PrintArray that specifies a single int[] parameter (which it finds at lines 2329) and sets up a call to that method. Similarly, when the compiler encounters the PrintArray call at line 17, it determines argument doubleArray's type (i.e., double[]), then attempts to locate a method named PrintArray that specifies a single double[] parameter (which it finds at lines 3238) and sets up a call to that method. Finally, when the compiler encounters the PrintArray call at line 19, it determines argument charArray's type (i.e., char[]), then attempts to locate a method named PrintArray that specifies a single char[] parameter (which it finds at lines 4147) and sets up a call to that method.
Study each PrintArray method. Note that the array element type (int, double or char) appears in two locations in each methodthe method header (lines 23, 32 and 41) and the foreach statement header (lines 25, 34 and 43). If we replace the element types in each method with a generic namewe chose E to represent the "element" typethen all three methods would look like the one in Fig. 26.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 PrintArray method that can display the elements of any array. The method in Fig. 26.2 will not compile, because its syntax is not correct. We declare a generic PrintArray method with the proper syntax in Fig. 26.3.
Figure 26.2. PrintArray method in which actual type names are replaced by convention with the generic name E.
1 static void PrintArray( E[] inputArray ) 2 { 3 foreach ( E element in inputArray ) 4 Console.Write( element + " " ); 5 6 Console.WriteLine( " " ); 7 } // end method PrintArray |
Generic Method Implementation |
Preface
Index
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Exception Handling
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Multithreading
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Web Services
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Data Structures
Generics
Collections
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types
Index