This is for the same reason that you expect this to happen:
int i = 1 - i;
The right part of the operator is calculated before the assignment and at the time of its calculation the variable has not yet been assigned.
If you think lambdas / delegates are changing things, consider the following statement:
int i = ((Action)() => 1 - i)();
Since you create a lambda before assigning i , it is possible that i can be used before any value is assigned to it. The fact that you do not expect this to happen in your case will not change the situation from the point of view of the compiler - you need to explicitly assign a value to the variable before using it. If this is a null value, then at least the compiler knows that you are considering the possibility that it will be null when you receive it.
As for your last question, then SessionEndingEventHandler is a delegate. So this will work fine:
var unsubscribed = SystemEvents.SessionEnding == null || !SystemEvents.SessionEnding.GetInvocationList().Contains(handler);
source share