Implementing Generic Methods

In Listing 14.1 you saw how to implement a method that receives a generic type parameter. Generic methods are something more. You can add the Of keyword to a generic method to parameterize the method, other than getting generic-type parameters. The following code provides an example, where two arrays of integers are swapped:

'Arrays are passed by reference in this casePublic Sub Swap(Of T1)(ByRef array1() As T1, ByRef array2() As T1)    Dim temp() As T1    temp = array1    array1 = array2    array2 = tempEnd Sub

You could even rewrite the preceding code snippet in a more abstract way, as follows, which still works well with arrays:

Public Sub Swap(Of T1)(ByRef ...

Get Visual Basic 2015 Unleashed 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.