Out
(Generics)
Specifies that a type parameter is covariant.
Instructions
Covariance lets you use a child class of the class specified by a generic parameter. This lets classes that implement variant traits convert automatically. See Covariance and Contravariance for more information.
Limits
You can use the modifier Out
with generic traits and events.
In a generic trait, you can declare a type parameter as covariant if you obey the instructions that follow:
- Use the type parameter only as a return type of trait methods. Do not use it as a type for method arguments. Do not use the type parameter as a generic constraint for the trait methods.
Examples
' Covariant trait.
Trait TCovariant[Out T]
End Trait
' Extending covariant trait.
Trait TExtCovariant[Out T] Does TCovariant[T]
End Trait
' Implementing covariant trait.
Class Sample[T] Does TCovariant[T]
End Class
Var parent As TCovariant[CParent] = CSample[CParent]
Var child As TCovariant[CChild] = CSample[CChild]
' You can assign child to parent because
' the TCovariant trait is covariant.
parent = child