Note: I edited this question to make it easier for other people with the same problem to get help here. To see the original question that fits better with some of the answers, check the edit history.
In a project I have a ExecutionManager class that can contain multiple ExecutionSlot's instances. The ExecutionSlot class have several public event fields like this:
public event EventHandlers.ObjectEventHandler<IPlugin> ExecuteCompleted;
For each of these events there is a matching event on ExecutionManager. The desired behavior is that every time on of the ExecutionSlot's raises an event, the matching event is also raised on the containing ExecutionManager.
The implmented solution was that whenever an ExecutionSlot was added to an ExecutionManager the ExectionManager would add its own events to the ExecutionSlot's like this:
executionSlot.ExecuteCompleted += ExecuteCompleted;
There is no need yet to remove an ExecutionSlot, so the events are never removed either.
The problem is that the event on ExecutionManager is not being raised. After confirming that an event was being reaised by an ExecutionSlot I found out that changing the above line to the following fixed the problem:
executionSlot.ExecuteCompleted += (sender, eventArgs) => ExecuteCompleted(sender, eventArgs);
And I could not figure out why, so my question was, what the difference was.
The reason for this difference was that the first adds the current listeners of the ExecutionManager's event to the ExecutionSlot's event. So any listeners added later will not be called when the event is raised. In contrast the latter solution uses a lambda to raise the ExecutionManager's event, which means that the listeners at the time of the event will be called.
The underlying reason for the first solution to fail, is that delegates are immutable. So when you add a new delegate to and event, you are actually creating a new delegate that contains the existing delegates and the added. So any reference to the delegates made before will not contain the newly added delegate.