VB.Net Tutorial/Generics/Generic Method — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:56, 26 мая 2010
Generic method maximum returns the largest of three objects
Module MaximumTest
Sub Main()
Console.WriteLine(5, Maximum(3, 4, 5))
Console.WriteLine(Maximum(6.6, 8.8, 7.7))
Console.WriteLine(Maximum("pear", "apple", "orange"))
End Sub
Public Function Maximum(Of T As IComparable(Of T))(ByVal x As T, ByVal y As T, ByVal z As T) As
T
Dim max As T = x
If y.rupareTo(max) > 0 Then
max = y
End If
If z.rupareTo(max) > 0 Then
max = z
End If
Return max
End Function
End Module
5 8.8 pear
Using generic methods to print arrays of different types
Module Tester
Sub Main()
Dim integerArray As Integer() = {1, 2, 3, 4, 5, 6}
Dim doubleArray As Double() = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}
Dim charArray As Char() = {"H"c, "E"c, "L"c, "L"c, "O"c}
PrintArray(integerArray)
PrintArray(doubleArray)
PrintArray(charArray)
End Sub
Public Sub PrintArray(Of E)(ByVal inputArray() As E)
For Each element As E In inputArray
Console.Write(element.ToString() & " ")
Next element
Console.WriteLine(vbCrLf)
End Sub
End Module
1 2 3 4 5 6 1.1 2.2 3.3 4.4 5.5 6.6 7.7 H E L L O