次の方法で共有


ジェネリック コレクション用インターフェイスの分散性の使用 (Visual Basic)

共変インターフェイスを使用すると、そのメソッドはインターフェイスで指定された型よりも多くの派生型を返すことができます。 反変インターフェイスを使用すると、そのメソッドは、インターフェイスで指定された型よりも派生型の少ないパラメーターを受け取ることができます。

.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) インターフェイスを実装しています。 ただし、EmployeeEmployeeを継承するため、このクラスを再利用して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

こちらも参照ください