In (Generic Modifier) (Visual Basic)


For generic type parameters, the In keyword specifies that the type parameter is contravariant.

Contravariance enables you to use a less derived type than that specified by the generic parameter. This allows for implicit conversion of classes that implement variant interfaces and implicit conversion of delegate types.

For more information, see Covariance and Contravariance (C# and Visual Basic).

You can use the In keyword in generic interfaces and delegates.

A type parameter can be declared contravariant in a generic interface or delegate if it is used only as a type of method arguments and not used as a method return type. ByRef parameters cannot be covariant or contravariant.

Covariance and contravariance are supported for reference types and not supported for value types.

In Visual Basic, you cannot declare events in contravariant interfaces without specifying the delegate type. Also, contravariant interfaces cannot have nested classes, enums, or structures, but they can have nested interfaces.

An interface that has a contravariant type parameter allows its methods to accept arguments of less derived types than those specified by the interface type parameter. For example, because in .NET Framework 4, in the IComparer<T> interface, type T is contravariant, you can assign an object of the IComparer(Of Person) type to an object of the IComparer(Of Employee) type without using any special conversion methods if Person inherits Employee.

A contravariant delegate can be assigned another delegate of the same type, but with a less derived generic type parameter.

The following example shows how to declare, extend, and implement a contravariant generic interface. It also shows how you can use implicit conversion for classes that implement this interface.

Imports System
Imports System.Diagnostics

Class MyProcess
    Inherits Process

    Public Sub [Stop]() 

    End Sub 'StopStop
End Class

Class StartNotePad

    Public Shared Sub Main(ByVal args() As String) 
        Dim p As New MyProcess()
        p.StartInfo.FileName = "notepad.exe"
        p.EnableRaisingEvents = True
        AddHandler p.Exited, AddressOf myProcess_HasExited

    End Sub

    Private Shared Sub myProcess_HasExited(ByVal sender As Object, ByVal e As System.EventArgs) 
        Console.WriteLine("Process has exited.")

    End Sub 'myProcess_HasExited
End Class

The following example shows how to declare, instantiate, and invoke a contravariant generic delegate. It also shows how you can implicitly convert a delegate type.

Public Overrides Function Equals(ByVal obj As Object) As Boolean 
    ' If Me and obj do not refer to the same type, then they are not equal.
    Dim objType As Type = obj.GetType()
    Dim meType  As Type = Me.GetType()
    If Not objType.Equals(meType) Then
        Return False
    End If 
    ' Return true if  x and y fields match.
    Dim other As Point = CType(obj, Point)
    Return Me.x = other.x AndAlso Me.y = other.y
End Function 
© 2015 Microsoft