//! public event Action<NewMailEventArgs> NewMail; //! Step #3: Define a method responsible for raising the event //! to notify registered objects that the event has occurred //! If this class is sealed, make this method private and nonvirtual protected virtual void OnNewMailReceived(NewMailEventArgs e) { // Copy a reference to the delegate field // now into a temporary field for thread safety Volatile.Read(ref NewMailReceived) ?.Invoke(this, e); }
//! Step #4: Define a method that translates the //! input into the desired event public void SimulateNewMail(String from, String to, String subject) { // Construct an object to hold the information we wish // to pass to the receivers of our notification var e = new NewMailEventArgs(from, to, subject); // Call our virtual method notifying our object that the event // occurred. If no type overrides this method, our object will // notify all the objects that registered interest in the event OnNewMailReceived(e); }