Option Strict On does not allow narrowing in implicit type conversions between the lambda expression and delegate '<delegatename>'

With Option Strict on, you cannot have a narrowing conversion between the data type of a parameter in a delegate and the corresponding parameter of a lambda expression assigned to a variable of that delegate type. For example, in the following code, delegate Del has one parameter of type Integer.

Delegate Function Del(ByVal p As Integer) As String

Therefore, the corresponding parameter of any lambda expression assigned to a variable of type Del can be an Integer or any data type for which there is a widening conversion from Integer.

' Valid.
Dim example1 As Del = Function(n As Integer) "Valid"
Dim example2 As Del = Function(n As Long) "Valid"

' Not valid.
Dim example3 As Del = Function(n As Short) "Not Valid"

Error ID: BC36662

To correct this error

  • Change the data type of the parameter in the delegate or the lambda expression so that the required widening relationship exists.

  • Do not specify parameter data types in the lambda expression. Types will be inferred from the corresponding parameters in the delegate.

    Dim example4 As Del = Function(n) "Valid"
    

See Also

Concepts

Lambda Expressions

Widening and Narrowing Conversions

Relaxed Delegate Conversion

Other Resources

Delegates in Visual Basic