c events are explained in detail with examples

  • 2020-06-19 11:38:14
  • OfStack

Events: If a type defines an event member, then the type can notify other objects that a specific event has occurred. For example, the Button class provides an event named Click. One or more objects in the application may want to receive notifications about this event in order to take action after Button is clicked.

Let's use an example to deepen our understanding of events: Suppose you are designing an E-mail application that the user might want to forward to a fax machine or other device when the E-mail arrives. When you build this application, you first design an MailManager type that receives incoming E-mail, and an MailManager type that publishes an NewMail event. Other types, such as Fax and Pager objects, can register their interest in this event.

The following code:

Step 1: Define the type to hold all additional information that needs to be sent to the receiver of the event notification


internal class NewMailEventArgs : EventArgs{
      private readonly String m_from,m_to,m_subject;

      Public NewMailEventArgs(string from,string to,string subject){
           m_from=from;m_to=to;m_subject=subject;  
        }  
      public string From{get{return m_from;}}
      Public string To{get{return m_to;}}
      Public string Subject{get{return m_subject;}}          
}

Note: EventArgs is just a base type that is inherited by other types. Many events have no additional information to pass, but in our scenario we need to pass mail information, so construct NewMailEventArgs.

Step 2: Define the event members


internal class MailManager{
    public event EventHandler<NewMailEventArgs> NewMail;
}
 Note: NewMail Is the name of the event. The type of event member is EventHandler<NewMailEventArgs>, So the method prototype must have the following form: 
void MethodName(Object sender,NewMailEventArgs e);

Step 3: Define the method responsible for raising the event to notify the registered object of the event


internal class MailManager{
    protected virtual void OnNewMail(NewMailEventArgs e){
     // For thread safety reasons, now copy the reference to the delegate field to 1 In a temporary field 
       EventHandler<NewMailEventArgs> temp = Interlocked.CompareExchange(ref NewMail,null,null);
       // Any method that registers an interest in an event notifies them 
       if(temp!=null) temp(this,e);  
  }
}

Step 4: Define methods that convert input into expected events


internal class MailManager{
      public void SimulateNewMail(string from,string to,string subject){
            NewMailEventArgs e = new NewMailEventArgs(from,to,subject);
            OnNewMail(e);
    }
}

Design to listen to the event type, next we use the Fax type to use the event, on the code


internal sealed Class Fax{
    public Fax(MailManager mm){
          mm.NewMail += FaxMsg;
     }
    // When the new email arrives, MailManager This method will be called 
   Private Void FaxMsg(object sender,NewMailEventArgs e){
        Console.WriteLine(" Events trigger ");
    }
   // So if I execute this method, Fax Object to NewMail The event logs out of its attention 
  Public Void Unregister(MailManager mm){
        mm.NewMail -= FaxMsg;
    }
}

Note: The C# compiler translates the += operator into the following code to add the object's attention to events:

mm.add_NewMail(new EventHandler < NewMailEventArgs > (this.FaxMsg));

This completes our example, triggering all the methods concerned with the mail class event when a new message is received, the FaxMsg method in the example, Fax. Examples are needed to help you understand the events.


Related articles: