Partager via


Out (Generic Modifier) (Visual Basic)

For generic type parameters, the Out keyword specifies that the type is covariant.

Remarks

Covariance enables you to use a more 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).

Rules

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

In a generic interface, a type parameter can be declared covariant if it satisfies the following conditions:

  • The type parameter is used only as a return type of interface methods and not used as a type of method arguments.

    Note

    There is one exception to this rule. If in a covariant interface you have a contravariant generic delegate as a method parameter, you can use the covariant type as a generic type parameter for this delegate. For more information about covariant and contravariant generic delegates, see Variance in Delegates (C# and Visual Basic) and Using Variance for Func and Action Generic Delegates (C# and Visual Basic).

  • The type parameter is not used as a generic constraint for the interface methods.

In a generic delegate, a type parameter can be declared covariant if it is used only as a method return type and not used for method arguments.

Covariance and contravariance are supported for reference types, but they are not supported for value types.

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

Behavior

An interface that has a covariant type parameter enables its methods to return more derived types than those specified by the type parameter. For example, because in .NET Framework 4, in IEnumerable<T>, type T is covariant, you can assign an object of the IEnumerabe(Of String) type to an object of the IEnumerable(Of Object) type without using any special conversion methods.

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

Example

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

' Covariant interface. 
Interface ICovariant(Of Out R)
End Interface 

' Extending covariant interface. 
Interface IExtCovariant(Of Out R)
    Inherits ICovariant(Of R)
End Interface 

' Implementing covariant interface. 
Class Sample(Of R)
    Implements ICovariant(Of R)
End Class 

Sub Main()
    Dim iobj As ICovariant(Of Object) = New Sample(Of Object)()
    Dim istr As ICovariant(Of String) = New Sample(Of String)()

    ' You can assign istr to iobj because 
    ' the ICovariant interface is covariant.
    iobj = istr
End Sub

The following example shows how to declare, instantiate, and invoke a covariant generic delegate. It also shows how you can use implicit conversion for delegate types.

' Covariant delegate. 
Public Delegate Function DCovariant(Of Out R)() As R

' Methods that match the delegate signature. 
Public Shared Function SampleControl() As Control
    Return New Control()
End Function 

Public Shared Function SampleButton() As Button
    Return New Button()
End Function 

Private Sub Test()

    ' Instantiating the delegates with the methods. 
    Dim dControl As DCovariant(Of Control) =
        AddressOf SampleControl
    Dim dButton As DCovariant(Of Button) =
        AddressOf SampleButton

    ' You can assign dButton to dControl 
    ' because the DCovariant delegate is covariant.
    dControl = dButton

    ' Invoke the delegate.
    dControl()
End Sub

See Also

Reference

In (Generic Modifier) (Visual Basic)

Concepts

Variance in Generic Interfaces (C# and Visual Basic)