I've always invoked events as so
void onSomeEvent(string someArg) {
var h = this.EventName;
if (h != null) {
h(this, new MyEventArgs(someArg));
}
}
Today VS 2015 tells me this can be simplified:
MyEvent?.Invoke(this, new MyEventArgs(someArg));
A few questions on this latter method, which I've not seen before:
- Presumably the
?
after the event name is a check if the handler is null? - Assuming the handler is not null,
.Invoke()
seems straightforward enough - I've used the first example for years and realize it prevents race conditions... presumably the
?.Invoke()
of the second example does so as well?