VB.Net/Generics/Generic Function

Материал из VB Эксперт
Перейти к: навигация, поиск

Generic Swap

<source lang="vbnet"> Option Explicit On Option Strict On Module Program

 Public Function Swap(Of T)(ByRef a As T, ByRef b As T) As T
   Console.WriteLine("T is a {0}.", GetType(T))
   Dim temp As T
   temp = a
   a = b
   b = temp
 End Function
 Sub DisplayBaseClass(Of T)()
   Console.WriteLine("Base class of {0} is: {1}.", GetType(T), GetType(T).BaseType)
 End Sub
 Sub Main()
   Dim a, b As Integer
   a = 10 : b = 40
   Swap(Of Integer)(a, b)
   Dim s1, s2 As String
   s1 = "Generics" : s2 = "Rock"
   Swap(Of String)(s1, s2)
   Dim b1, b2 As Boolean
   b1 = True : b2 = False
   Swap(b1, b2)
   DisplayBaseClass(Of Boolean)()
   DisplayBaseClass(Of String)()
   DisplayBaseClass(Of Integer)()
 End Sub

End Module


 </source>