What is the lifetime of a lambda expression?
Here is my problem: I have a button1.Click event subscribed by a lambda expression. I don’t know if I need need to unsubscribe it (which is not easy because it is anonymous)? Or I don’t have to, because it is in the same lifetime of the control(button1) it is attached?
button1.Click += (s, e) => { /*Do something; */};
Since the lambda expression is associated with the button,it will live as long as the button is not destroyed from memory.for your question
Lambda expressions can be saved to a EventHandler delegate,which gives you the option to access the lambda and unsubscribe from the event.Here is the code.
probably the best explanation on this topic here Should I unsubscribe from events?