SoFunction
Updated on 2025-03-07

Detailed explanation of c# event usage example

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

Let's use an example to deepen our understanding of events: Assuming that you are now designing an email application, when the email arrives, the user may want to forward the email to a fax machine or other device. When building this application, first design a MailManager type, which is responsible for receiving incoming emails, and the MailManager type publishes a NewMail event. Other types, such as Fax and Pager objects, can register their attention to this event.

The following code:

Step 1: Define the type to accommodate all additional information that needs to be sent to the event notification recipient

Copy the codeThe code is as follows:

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 allows other types to inherit. Many events have no additional information to be passed, but in our scenario, we need to pass email information, so we construct NewMailEventArgs.

Step 2: Define event members

Copy the codeThe code is as follows:

internal class MailManager{
    public event EventHandler<NewMailEventArgs> NewMail;
}
Note: NewMail is the name of this 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 an event to notify the registered object of the event

Copy the codeThe code is as follows:

internal class MailManager{
    protected virtual void OnNewMail(NewMailEventArgs e){
//For thread safety reasons, now copy the reference of the delegate field into a temporary field
       EventHandler<NewMailEventArgs> temp = (ref NewMail,null,null);
//Not any method to register attention to the incident
       if(temp!=null) temp(this,e); 
  }
}

Step 4: Define the method to convert the input into expected events

Copy the codeThe code is as follows:

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

Design the listening event type. Let’s use the Fax type to use the event.

Copy the codeThe code is as follows:

internal sealed Class Fax{
    public Fax(MailManager mm){
          += FaxMsg;
     }
//MailManager will call this method when the new email arrives
   Private Void FaxMsg(object sender,NewMailEventArgs e){
("Event trigger");
    }
//Execute this method, the Fax object will log out of its attention to it to the NewMail event.
  Public Void Unregister(MailManager mm){
        -= FaxMsg;
    }
}

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

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

This way our example is done, when a new message is received, all methods that focus on the mail-like event will be triggered, i.e. the FaxMsg method in Fax in the example. Examples need to be helpful for everyone to understand the event.