Hello Friends, In this tutorial we are going to learn Events in C#.  Events are members of the class that is declared in the class and raised by that class

What are Events?

Events are members of the class that is declared in the class and raised by that class. The Events are declared in the class and associated with the event handlers using the delegates. Events and delegates are tightly coupled because event handling needs a delegate for the invocation of events. Events use the publisher-subscriber model.

Publisher:

A publisher is a class that contains the definition of the event and delegate. The publisher class object is responsible to invoke the event and notifies to other objects.

Subscriber:

A Subscriber is a class that is responsible to provide event handler to the event class.
The delegate in the publisher class invokes the method (event handler) of the subscriber class.
So, In short, the class that raises the event is called publisher class and that receives and
provides a handler for the event is called the subscriber.

Declaring Events:

To Declare an Event inside a Publisher class first we need to declare a delegate type for that event. For Example.

public delegate void MyDelegate;

public event MyDelegate evtMyEvent;

Example of Events:

Let’s create an example of Events. Suppose I need to get Employee details Like Name, Address, and Age where Age of Employee should be greater than 18. If the age of Employee is less than 18 then a message should be raised that “You are not eligible to proceed”. We can achieve this requirement using Events.

 Create a Console Application called EventDemo.

Write the following code in Program.cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EventDemo
{
class Program
{
static void Main(string[] args)
{
Employee objEmployee = new Employee();
objEmployee.evtValidateAge += new Employee.delValidateAge(AgeValidationMessage);
objEmployee.GetEmployeeDetails();
Console.ReadLine();
}
public static void AgeValidationMessage(int iAge)
{
Console.WriteLine("You are not eligible to proceed");
}
}
public class Employee
{
public delegate void delValidateAge(int iAge );
public event delValidateAge evtValidateAge;
public void GetEmployeeDetails()
{
Console.WriteLine("Enter Name of Employee");
string sName = Console.ReadLine();
Console.WriteLine("Enter Address of Employee");
string sAddress = Console.ReadLine();
Console.WriteLine("Enter Age of Employee");
int iAge = Convert.ToInt32(Console.ReadLine());
if (iAge < 18 && evtValidateAge != null)
evtValidateAge(iAge);
else
{
Console.WriteLine("Welcome {0}", sName);
}

}
}
}

In the above program, you can see have declared a delegate delValidateAge that accepts an Age Parameter. In GetEmployeeDetails method we are accepting the Details for the Employee where if the Employee Age is less than 18 the event will be raised.

In the main method, we have associate Deleage with the Event using the following code.

Employee objEmployee = new Employee();
objEmployee.evtValidateAge += new Employee.delValidateAge(AgeValidationMessage);

The Delegate delValidateAge accepts AgeValidationMessage as a parameter.

Now run the application look over the output window.

Events

View More:

Conclusion:

I hope you understand the concept of Events in C#. I would appreciate your Feedback, Comments, and Suggestions.

Thank You.

Leave a Comment