I always triggered events like that
void onSomeEvent(string someArg) { var h = this.EventName; if (h != null) { h(this, new MyEventArgs(someArg)); } }
Today, VS 2015 tells me that this can be simplified:
MyEvent?.Invoke(this, new MyEventArgs(someArg));
A few questions on this last method that I have not seen before:
- Supposedly
? after the event name is a check if the handler is null? - Assuming the handler is non-zero,
.Invoke() seems simple enough - I have used the first example for many years and realized that it prevents race conditions ... presumably what
?.Invoke() second example does this too?
c # events
jleach
source share