Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Interfejs kowariantny pozwala swoim metodom zwracać więcej typów pochodnych niż określone w interfejsie. Interfejs kontrawariantny umożliwia jego metodom akceptowanie parametrów mniej pochodnych typów niż określone w interfejsie.
W programie .NET Framework 4 kilka istniejących interfejsów stało się kowariantne i kontrawariantne. Należą do IEnumerable<T> nich: i IComparable<T>. Umożliwia to ponowne użycie metod, które działają z ogólnymi kolekcjami typów bazowych dla kolekcji typów pochodnych.
Aby uzyskać listę interfejsów wariantów w programie .NET Framework, zobacz Variance in Generic Interfaces (Visual Basic) (Wariancja w interfejsach ogólnych (Visual Basic).
Konwertowanie kolekcji ogólnych
W poniższym przykładzie przedstawiono zalety obsługi kowariancji w interfejsie IEnumerable<T> . Metoda PrintFullName akceptuje kolekcję IEnumerable(Of Person) typu jako parametr. Można jednak użyć go ponownie dla kolekcji typu IEnumerable(Of Person), ponieważ Employee dziedziczy Person.
' Simple hierarchy of classes.
Public Class Person
Public Property FirstName As String
Public Property LastName As String
End Class
Public Class Employee
Inherits Person
End Class
' The method has a parameter of the IEnumerable(Of Person) type.
Public Sub PrintFullName(ByVal persons As IEnumerable(Of Person))
For Each person As Person In persons
Console.WriteLine(
"Name: " & person.FirstName & " " & person.LastName)
Next
End Sub
Sub Main()
Dim employees As IEnumerable(Of Employee) = New List(Of Employee)
' You can pass IEnumerable(Of Employee),
' although the method expects IEnumerable(Of Person).
PrintFullName(employees)
End Sub
Porównywanie kolekcji ogólnych
W poniższym przykładzie przedstawiono zalety obsługi kontrawariancji w interfejsie IComparer<T> . Klasa PersonComparer implementuje interfejs IComparer(Of Person). Można jednak ponownie użyć tej klasy, aby porównać sekwencję obiektów Employee typu, ponieważ Employee dziedziczy Personelement .
' Simple hierarchy of classes.
Public Class Person
Public Property FirstName As String
Public Property LastName As String
End Class
Public Class Employee
Inherits Person
End Class
' The custom comparer for the Person type
' with standard implementations of Equals()
' and GetHashCode() methods.
Class PersonComparer
Implements IEqualityComparer(Of Person)
Public Function Equals1(
ByVal x As Person,
ByVal y As Person) As Boolean _
Implements IEqualityComparer(Of Person).Equals
If x Is y Then Return True
If x Is Nothing OrElse y Is Nothing Then Return False
Return (x.FirstName = y.FirstName) AndAlso
(x.LastName = y.LastName)
End Function
Public Function GetHashCode1(
ByVal person As Person) As Integer _
Implements IEqualityComparer(Of Person).GetHashCode
If person Is Nothing Then Return 0
Dim hashFirstName =
If(person.FirstName Is Nothing,
0, person.FirstName.GetHashCode())
Dim hashLastName = person.LastName.GetHashCode()
Return hashFirstName Xor hashLastName
End Function
End Class
Sub Main()
Dim employees = New List(Of Employee) From {
New Employee With {.FirstName = "Michael", .LastName = "Alexander"},
New Employee With {.FirstName = "Jeff", .LastName = "Price"}
}
' You can pass PersonComparer,
' which implements IEqualityComparer(Of Person),
' although the method expects IEqualityComparer(Of Employee)
Dim noduplicates As IEnumerable(Of Employee) = employees.Distinct(New PersonComparer())
For Each employee In noduplicates
Console.WriteLine(employee.FirstName & " " & employee.LastName)
Next
End Sub