For Sample ….
SampleClass :
public class SampleClass
{
public delegate void BeforeEditorHandle();
public event BeforeEditorHandle OnBeforeEditor;
}
MainMethod
static void Main(string[] args)
{
SampleClass sc = new SampleClass();
// Add Event
sc.OnBeforeEditor +=new SampleClass.BeforeEditorHandle(sc_OnBeforeEditor);
// Remove Event
sc.OnBeforeEditor -= new SampleClass.BeforeEditorHandle(sc_OnBeforeEditor);
}
And , if I add the event by dynamic like this …↓
sc.OnBeforeEditor += () => { };
Should I remove the event like ↓
sc.OnBeforeEditor -= () => { };
But I think this is very ugly when I have too much sources in the event….
Can anybody tell me the best way to remove the event please ?
I’m pretty sure your code here won’t work:
This is because restating the lambda creates a new different lambda.
You need to store the old reference and use it to unsubscribe:
For easier unsubscribing you can collect your event handlers in a collection (For example
List<BeforeEditorHandle>).