I found different syntax version of firing an event in c#, and I cant really figure out what exactly the difference is between:
if (OnMyEvent!= null)
OnMyEvent(this, new MyEventEventArgs());
and:
OnMyEvent.Invoke(this, new MyEventEventArgs());
especially if I dont have any thread context changes?
Thanks in advance!
Using Reflector or ildasm, you can see that these compile (modulo the
nullcheck) into the exact same thing (a call toInvoke()).As for what’s desirable, you should always check for
nulland you’ll more often see the invocation syntax over theInvoke()syntax.