Lambda Expressions and Event Subscriptions

I heard that if lambda expressions are used to subscribe to an event, this creates a weak link to the code of the event handler, so you do not need to explicitly unsubscribe from the event when the subscriber dies / is no longer interesting. It's true? For example.

aPersion.PropertyChanged += (s, e) =>
                    {
                        if (e.PropertyName == "Name")
                        {
                            this.Name = this.TheController.Name;
                        }
                    };
+3
source share
2 answers

No, in the context of subscribing to events, lambda expressions are simply delegates for all purposes and goals and, therefore, remain prone to problems with lost listeners. So no, this is definitely not a weak link.

There are many approaches to using weak links to work on this problem, which are well summarized in this article by Damien Guard

+2

, . Lambdas ( , ). , , , . , .

, , , : aPersion ( ) , // .

+2

Source: https://habr.com/ru/post/1723089/


All Articles