c# - Why is an additional if statement to check if an event is null before invoking it? -
i going through code below:
public delegate void personarrivedeventhandler(string personname, byte[] personid); public class sclib { public event personarrivedeventhandler personrrived; public sclib() { // simulate person arrived after 2000 milli-seconds. task.run(() => { system.threading.thread.sleep(2000); onpersonarrived("personname", new byte[] { 0x20, 0x21, 0x22 }); }); } protected virtual void onpersonarrived(string smartcardreadername, byte[] smartcardid) { if (this.personarrived != null) { personarrived(personname, personid); } } }
but, don't know significance of line, if (this.personarrived != null)
.
why check done here? there significance of if statement
here? removed line , ran program , works before.
thanks.
if event not subscribed consumer of class
invoking event
raise exception personarrived
null if not subscribed.
Comments
Post a Comment