25.3. Generic Method Implementation

The overloaded methods of Fig. 25.1 can be more compactly and conveniently coded using a single generic method. You can write a single generic method declaration that can be called at different times with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately.

Figure 25.3 reimplements the application of Fig. 25.1 using a single generic Print-Array method (lines 19–25). Note that the PrintArray method calls in lines 11, 13 and 15 are identical to those in Fig. 25.1, the outputs of the two applications are identical and the code in Fig. 25.3 is 18 lines shorter than the code in Fig.25.1. As illustrated in Fig. 25.3, generics ...

Get Visual Basic 2005 for Programmers: Deitel Developer Series, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.