使用泛型集合介面中的變異數 (Visual Basic)

Covariant 介面允許其方法傳回與介面中指定的類型相比,其衍生程度較大的類型。 Contravariant 介面允許其方法接受與介面中指定的參數相比,其類型衍生程度較小的參數。

在 .NET Framework 4 中,有數個現有介面已變成 Covariant 和 Contravariant。 這些結構包括 IEnumerable<T>IComparable<T>。 因此,您可以針對衍生類型的集合,重複使用搭配基底類型之泛型集合運作的方法。

如需 .NET Framework 的變體介面清單,請參閱泛型介面的變異數 (Visual Basic)

轉換泛型集合

下列範例說明在 IEnumerable<T> 介面中支援共變數的好處。 PrintFullName 方法接受 IEnumerable(Of Person) 類型的集合作為參數。 不過,您可以重複用於 IEnumerable(Of Person) 類型的集合,因為 Employee 會繼承 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

比較泛型集合

下列範例說明在 IComparer<T> 介面中支援反變數的好處。 PersonComparer 類別會實作 IComparer(Of Person) 介面。 不過,您可以重複使用此類別來比較一連串類型為 Employee 的物件,因為 Employee 會繼承 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 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

另請參閱