C# Event Handler Example
C# Event Handler:
An event is a signal that an item sends out to indicate that an action has taken place. In.NET, events adhere to the observer design pattern.
Publisher is the class that raises events, while Subscriber is the class that receives the notification. An event can have more than one subscription. Publishers usually raise an event in response to an activity. Subscribers who wish to receive notifications when an action takes place ought to register and manage events.
An event in C# is a delegate that has been wrapped. That depends on who is delegating. The delegate defines the subscriber class's event handler method's signature.
When a Publisher-Subscriber design pattern is applied, events and event handlers are typically utilized when a specific class experiences an event and wants to inform other classes about it. A conditional event in the Publisher class is called, and subscribers are notified according to their subscription. The publisher can be made aware of who receives notifications, which is one of its main advantages.
Events are things that take place within a program. A class or object can alert other classes or objects to an event by using events.
C# Events
The publisher is the class that raises (or despatches) the event.
- Subscribers are the classes that receive (or handle) the event. Additionally, event handlers are frequently the name given to the class methods that deal with events.
- The term "publisher/subscriber" describes this trend. According to this structure, the subscribers choose how to handle the event, and the publisher decides when to raise it.
Assume you have an Order class and that its Create() function generates new orders:
Code:
class Order
{
public void Create()
{
Console.WriteLine("Created the order");
}
}
Plus, two more classes that include email and SMS capabilities:
Code:
class Email
{
public static void Send()
{
Console.WriteLine("email should be sent");
}
}
class Message
{
public static void Send()
{
Console.WriteLine("Message should be sent");
}
}
You should send the consumer a Message and email as soon as an order is created. You might generate the following code to accomplish it:
Code:
class Order
{
public void Create()
{
Console.WriteLine("Created the order");
Email.Send();
Message.Send();
}
}
Afterward, you must alter the Create() method if you wish to perform other actions when order is formed. Furthermore, it is not a smart design for the Order class to be dependent on the Email and Message classes. You can use the publisher/subscriber pattern to fix this:
- The publisher belongs to the Order class.
- The subscribers are the Email and Message classes.
The Order object notifies the Email and Message classes to send an email and a Message when an order is created.
Announcement of an Event:
When an order is created, the OnCreated event is declared as follows:
delegate void OrderEventHandler();
class Order
{
public event OrderEventHandler OnCreated;
public void Create()
{
Console.WriteLine("We have created the order");
}
}
An event must be declared inside the class because it is a member of the class. In this case, the event is made public so that handlers from other classes can register for it. Additionally, the delegate type linked to the event and the event handlers must match.
Bringing up a situation
Invoking a method is equivalent to raising an event. Null is the state of an event that has no event handlers. As a result, you must compare an event to null before elevating it.
The Create() method's OnCreated event is raised by the following:
class Order
{
public event OrderEventHandler OnCreated;
public void Create()
{
Console.WriteLine("We have created the order");
if(OnCreated != null)
{
OnCreated();
}
}
}
EventHandler:
When you use events, you don't need to define a new delegate type because EventHandler C# gives you access to the standard EventHandler delegate type.
The declaration of the EventHandler delegate type is displayed as follows:
public delegate void EventHandler(object sender, EventArgs e);
With this particular delegate type:
- The sender references the object that raised the event.
- The state information, which might be anything relevant to the program, is stored in the EventArgs object.
It's critical to realize that event handlers that do not require data passing from publisher to subscriber are not intended users of EventArgs. You must define a class that is inherited from the EventArgs class if you wish to pass the data.
The program that follows demonstrates how to use the order-created event's EventHandler:
class Order
{
public event EventHandler OnCreated;
public void Create()
{
Console.WriteLine("We have created the order");
if(OnCreated != null)
{
OnCreated(this, EventArgs.Empty);
}
}
}
class Email
{
public static void Send(object sender, EventArgs e)
{
Console.WriteLine($"Send an email");
}
}
class Message
{
public static void Send(object sender, EventArgs e)
{
Console.WriteLine($"Send Meassage");
}
}
class Program
{
static void Main(string[] args)
{
var order = new Order();
order.OnCreated += Email.Send;
order.OnCreated += Message.Send;
order.Create();
}
}