سؤال

I have the following code attaching event handler:

this.btnOK.Click += (s,e) => { MessageBox.Show("test");  };

Can I unsubscribe that lambda expression from the vent?

هل كانت مفيدة؟

المحلول

Why don't just save the assigned lambda?

 EventHandler lambda = (s,e) => { MessageBox.Show("test");  };

 ...

 this.btnOK.Click += lambda;

 ... 

 this.btnOK.Click -= lambda;

نصائح أخرى

You cannot un-assign that event because it is a anonymous method.

Give it a name and you are ready to unsubscribe.

you can do :

this.btnOK.Click = null;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top