Lambda expression will not be removed from this event handler

Lambda expression will not be removed from this event handler. Assign the lambda expression to a variable and use the variable to add and remove the event.

When lambda expressions are used with event handlers, you may not see the behavior you expect. The compiler generates a new method for each lambda expression definition, even if they are identical. Therefore, the following code displays False.

Module Module1

    Sub Main()
        Dim fun1 As ChangeInteger = Function(p As Integer) p + 1
        Dim fun2 As ChangeInteger = Function(p As Integer) p + 1
        Console.WriteLine(fun1 = fun2)
    End Sub

    Delegate Function ChangeInteger(ByVal x As Integer) As Integer

End Module

When lambda expressions are used with event handlers, this may cause unexpected results. In the following example, the lambda expression added by AddHandler is not removed by the RemoveHandler statement.

Module Module1

    Event ProcessInteger(ByVal x As Integer)

    Sub Main()

        ' The following line adds one listener to the event.
        AddHandler ProcessInteger, Function(m As Integer) m

        ' The following statement searches the current listeners 
        ' for a match to remove. However, this lambda is not the same
        ' as the previous one, so nothing is removed.
        RemoveHandler ProcessInteger, Function(m As Integer) m

    End Sub
End Module

By default, this message is a warning. For more information about how to hide warnings or treat warnings as errors, see Configuring Warnings in Visual Basic.

Error ID: BC42326

To correct this error

  • To avoid the warning and remove the lambda expression, assign the lambda expression to a variable and use the variable in both the AddHandler and RemoveHandler statements, as shown in the following example.

    Module Module1
    
        Event ProcessInteger(ByVal x As Integer)
    
        Dim PrintHandler As ProcessIntegerEventHandler
    
        Sub Main()
    
            ' Assign the lambda expression to a variable.
            PrintHandler = Function(m As Integer) m
    
            ' Use the variable to add the listener.
            AddHandler ProcessInteger, PrintHandler
    
            ' Use the variable again when you want to remove the listener.
            RemoveHandler ProcessInteger, PrintHandler
    
        End Sub
    End Module
    

See Also

Concepts

Lambda Expressions

Relaxed Delegate Conversion

Events and Event Handlers