Action<object, EventArgs> could not be cast to EventHandler?

Viewed 12600

I was wiring up an event to use a lambda which needed to remove itself after triggering. I couldn't do it by inlining the lambda to the += event (no accessable variable to use to remove the event) so i set up an Action<object, EventArgs> variable and moved the lambda there. The main error was that it could not convert an Action<object, EventArgs> to an EventHandler. I thought lambda expressions were implicitly convertable to event handlers, why doesn't this work?

5 Answers

Declare your event as

public event Action<object, EventArgs> e;

Then you can directly add your action:

Action<object, EventArgs> a = something;
e += a;
Related