c# - What is the purpose of using myevent(this,EventArgs.Empty)? -


i learning delegates , events in csharp.i have following set of codes:

using system; using system.collections.generic; using system.text;  public delegate void mydel(object sender, eventargs e);   class event1 {     public event mydel myevent;      public void onfive()     {         console.writeline("i onfive event");         console.readkey();         if (myevent != null)         {              myevent(this,eventargs.empty);         }     } }  public class test {     public static void main()     {         event1 e1 = new event1();         e1.myevent += new mydel(fun1);          random ran = new random();         (int = 0; < 10; i++)         {             int rn = ran.next(6);              console.writeline(rn);             console.readkey();              if (rn == 5)             {                 e1.onfive();              }         }     }      public static void fun1(object sender, eventargs e)     {         console.writeline(" surplus function called due use of '+=' ");         console.readkey();     } } 

whenever put following lines in comment fun1() function not called.why it so?

if (myevent != null)  {    myevent(this,eventargs.empty);  } 

what purpose of these lines?

that bit of code raises event. if event not raised event handler not executed.

a delegate object refers method , event sort of collection of delegates. if don't add handlers event there no collection, hence check null. if event not null means handlers have been registered. line inside if statement raises event, means invoking each of delegates in collection. each delegate invoked, method refers executed. method gets executed event handler.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -