Why does this declaration+assignment cause an error:
// Use of unassigned local variable 'handler'.
SessionEndingEventHandler handler = (sender, e) => { isShuttingDown = true; SystemEvents.SessionEnding -= handler; };
while this does not:
SessionEndingEventHandler handler = null;
handler = (sender, e) => { isShuttingDown = true; SystemEvents.SessionEnding -= handler; };
In is intuitive that the first statement should cause an error but not immediately clear why the second one is not.
Furthermore, how could I tell if the SystemEvents.SessionEnding
event has been actually unsubscribed after the call to handler(null, null)
? The GetInvocationList
only works with delegates.
SystemEvents.SessionEnding += handler;
handler(null, null);