Custom Events in C#

In this article I shall demonstrate how to create custom events in C#. Before going through this article I recommend you get some knowledge of Delegates.

Example


In this example I have created a custom event handler for displaying Employee data. This event is raised when a new Employee is added to a List.

Step 1: Create a custom class named EmployeeArgs.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace CustomEvents

{

    public delegate void EmployeeAddDelegate(EmployeeArgs e);

   public class EmployeeArgs : EventArgs

    {

       public Employee _employee;

       public EmployeeArgs(Employee employee)

       {

           this._employee = employee;

       }

    }

}

EmployeeArgs is inherited from the predefined class EventArgs in the .Net Framework.

A delegate is declared with EmployeeArgs as the parameter as in the following:

public delegate void EmployeeAddDelegate(EmployeeArgs e);

Step 2: Create a Listener class for our custom event. Our handler will call the function ShowEmployees as in the following:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace CustomEvents

{

   public  class EmployeeListener

    {

       public void ShowEmployees(EmployeeArgs e)

       {

           // Call the model class to display the results on screen.

           Employee.ShowEmployees(e);

       }

    }

}

Step 3: Create an Employee Class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace CustomEvents

{

    public class Employee

    {

        public int ID { get; set; }

        public string Name { get; set; }

 

        public static void ShowEmployees(EmployeeArgs e)

        {

            Console.WriteLine("New Employee Added...");

            Console.WriteLine("Employee ID = {0}, Employee Name = {1} ", e._employee.ID, e._employee.Name);

        }

    }

}

Step 4: Construct the main class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace CustomEvents

{

    class Program

    {

        public static event EmployeeAddDelegate _employeeEventHandler;

        public static List<Employee> lstEmployees = null;

        public static EmployeeListener employeeListener = null;

        public static Employee emp1, emp2;

 

        static void Main(string[] args)

        {

            lstEmployees = new List<Employee>();

            employeeListener = new EmployeeListener();

            // Assigning the function ShowEmployees to the event handler.

            _employeeEventHandler += new EmployeeAddDelegate(employeeListener.ShowEmployees);

            //Initialising Employees

            emp1 = new Employee { ID = 10456, Name = "Madhav" };

            emp2= new Employee { ID = 15621, Name = "Praveen" };

 

           //Add the employees

            AddEmployees(emp1);

            AddEmployees(emp2);

            Console.ReadLine();

        }

 

        private static void AddEmployees(Employee employee)

        {

            if (employee != null)

            {

                EmployeeArgs e = new EmployeeArgs(employee);

                lstEmployees.Add(employee);

                //Event handling call

                OnEmployeeAdd(e);

            }

        }

        private static void OnEmployeeAdd(EmployeeArgs e)

        {

 //Event Handler is raised. Here ShowEmployees function in EmployeeListener is     raised.

// Eventhandler calls the ShowEmployees function in EmployeeListener class.

 

            if (_employeeEventHandler != null)

            {

                _employeeEventHandler(e);

            }

        }

    }

}

I have provided comments in the main class in such a way that it helps you understand the logic and its implementation.

Please find the attached source code for a better understanding.

Output

Event

Happy Coding.