C# – Events

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts. Events are used for inter-process communication.

Using Delegates with Events

The events are declared and raised in a class and associated with the event handlers using delegates within the same class or some other class. The class containing the event is used to publish the event. This is called the publisherclass. Some other class that accepts this event is called the subscriber class. Events use the publisher-subscriber model.

A publisher is an object that contains the definition of the event and the delegate. The event-delegate association is also defined in this object. A publisher class object invokes the event and it is notified to other objects.

A subscriber is an object that accepts the event and provides an event handler. The delegate in the publisher class invokes the method (event handler) of the subscriber class.

Declaring Events

To declare an event inside a class, first a delegate type for the event must be declared. For example,

public delegate void WorkPerformed(int hours);

Next, the event itself is declared, using the event keyword:

//Defining event based on the above delegate
public event BoilerLogHandler BoilerEventLog;

The preceding code defines a delegate named BoilerLogHandler and an event named BoilerEventLog, which invokes the delegate when it is raised.

Example 1

using System;

namespace DelegatesAndEvents {

   public delegate void WorkPerformed(int hours);
   class Program {
      static void Main(string[] args) {
         WorkPerformed del1 = new WorkPerformed(WorkPerformed1);
         WorkPerformed del2 = new WorkPerformed(WorkPerformed2);
         WorkPerformed del3 = new WorkPerformed(WorkPerformed3);


      static void WorkPerformed1(int hours) {
         Console.WriteLine("WorkPerformed1 called " + hours.ToString() + " hrs work done.");

      static void WorkPerformed2(int hours) {
         Console.WriteLine("WorkPerformed2 called " + hours.ToString() + " hrs work done.");

      static void WorkPerformed3(int hours) {
         Console.WriteLine("WorkPerformed3 called " + hours.ToString() + " hrs work done.");

When the above code is compiled and executed, it produces the following result:

WorkPerformed1 called 5 hrs work done.
WorkPerformed2 called 8 hrs work done.
WorkPerformed3 called 3 hrs work done.

Example 2

This example provides a simple application for troubleshooting for a hot water boiler system. When the maintenance engineer inspects the boiler, the boiler temperature and pressure is automatically recorded into a log file along with the remarks of the maintenance engineer.

using System;

namespace DelegatesAndEvents {

   delegate void MyDelegate(string s);
   class Program {
      public static void Main(string[] args) {
         MyDelegate myDelegate1, myDelegate2, myDelegate3, myDelegate4;

         // Create the delegate that references Hello method:
         myDelegate1 = new MyDelegate(Hello);

         // Create the delegate that references Goodbye method
         myDelegate2 = new MyDelegate(Goodbye);

         // The two delegates, myDelegate1 and myDelegate2, are composed to form myDelegate3: 
         myDelegate3 = myDelegate1 + myDelegate2;

         myDelegate4 = myDelegate3 - myDelegate1;

         Console.WriteLine("calling delegate myDelegate1:");
         Console.WriteLine("calling delegate myDelegate2:");
         Console.WriteLine("calling delegate myDelegate3:");
         Console.WriteLine("calling delegate myDelegate4:");

      public static void Hello(string s) {
         Console.WriteLine("  Hello, {0}!", s);

      public static void Goodbye(string s) {
         Console.WriteLine("  Goodbye, {0}!", s);

When the above code is compiled and executed, it produces the following result:

calling delegate myDelegate1:
  Hello, A!
calling delegate myDelegate2:
  Goodbye, B!
calling delegate myDelegate3:
  Hello, C!
  Goodbye, C!
calling delegate myDelegate4:
  Goodbye, D!

Leave a Reply

Your email address will not be published. Required fields are marked *