共用方式為


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

covariant 介面可讓其方法傳回的衍生類型比介面中指定的類型還要多。 Contravariant 介面可讓其方法接受的衍生型別參數,比介面中指定的參數還要少。

在 .NET Framework 4 中,數個現有的介面會變成共變數和反變數。 這些包括 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

另請參閱