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

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -