Event Handling in c#
Event handling in the C# programming language is a program-generated task process or mechanism that allows a particular portion publisher (event creator) of an existing C# program to indicate to another portion subscriber/user of the system that some system event or task generated action has been performed in the existing program. Event handling in the C# programming language is commonly used in graphical user interface (GUIs) based applications to respond to user actions (e.g., creating button clicks) by system events in the backend, or in applications where multiple individual system, web, or application-based components in a user-defined application form or software need to communicate with each other via events.

System events created in C# programming are completely based on delegates, and provide a method for a class or class-defined object in the event system to perform an action or event task when an action or event occurs (e.g., a user clicks on a button or a change in application state). All these types of action events provide a method for performing a task. The publisher of a system event in a program initiates the event, and subscribers handle or manage the event by providing event handler methods.
Key concepts of event handling in C# programming.
- System event – An event in C# programming is a message or display information that indicates that something has been generated or triggered in the program. This action or event is typically triggered by a system event action, such as a button press in an application, a modification to existing data, or a system timer ending.
- Delegate-based – In C# programs, a delegate is used to declare or define the signature of a system-generated event handler method (i.e., a user- or system-created method that will handle or manage events in the current application).
- Event handler procedure – This is a system-based event method that immediately responds to an event generated by the event creator (publisher). It is usually added to an event via the += operator.
- Publisher and Subscriber Roles – A system-based publisher is a class object that raises events in the current system, and a subscriber is a class object that listens for events in the current application and responds to them when they are generated in the system.
Basic syntax of event handling in C#.
First, define a delegate.
First, before creating a system or application event, you must define a delegate type. that properly matches the signature of the class method handling the event.
public delegate void Sample SampleEventHandler(string message);
This event delegate defines a method signature that returns a void data type and accepts a string parameter value as input.
Define a system event.
An event in a program is declared using the event keyword, followed by defining its delegate type.
public event MyEventHandler OnMessageReceived;
Raise a system event.
To trigger or raise an event in an application or system, C# users use the Invoke() method (or shorthand ?.Invoke()) on a custom event, passing the required system parameter values. OnMessageReceived?.Invoke(“Hi, Welcome to Vcanhelpsu”);
Subscribe to a system event.
To handle or manage events in an existing system or application, C# users must manually add a method (such as an event handler) to a custom event. The event you subscribe to is called.
OnMessageReceived += HandlerMethod;
The event handler concept in C# is a system.
An event handler in a C# program is a method that automatically responds when an event is triggered in the current program.
public void HandlerMethod(string message)
{
Console.WriteLine(message);
}
Complete example of a simple event handling in a C# program.
Using System;
public delegate void SampleEventHandler(string info); // here we declare the event delegate
public class publisher
{
// here we declare the event based on the delegate method
public event SampleEventHandler OnMessageReceived;
// here we define a Method to raise (fire) the event or task
public void RaiseEvent(string info)
{
// here it Trigger when the event if it has subscribers
OnMessageReceived?.Invoke(info);
}
}
public class subscriber
{
// here we define an Event handler method
public void HandleEvent(string info)
{
Console.WriteLine(“Status of Event task – ” + info);
}
}
public class program
{
static void Main()
{
// here we Create a publisher and subscriber instances
Publisher publisher = new Publisher();
Subscriber subscriber = new Subscriber();
// Here we subscribe to the event
publisher.OnMessageReceived += subscriber.HandleEvent;
// Here we raise the system event when it occurs
publisher.RaiseEvent(“Welcome to, Vcanhelpsu”); // Result is – Status of event task – Welcome to, Vcanhelpsu
publisher.RaiseEvent(“Vcanhelpsu, is an Edtech platform”); // Result is – Status of event task – Vcanhelpsu, is an Edtech platform
}
}
Explaining a simple event handling program.
- In this program, the SampleEventHandler delegate indicates the signature of the event handler method.
- In this program, an event, OnMessageReceived, is defined in the Publisher class, which uses the SampleEventHandler delegate.
- Similarly, the Subscriber class contains a class method called HandleEvent that will respond immediately when a system event is raised.
- In the Main() program function, the Subscriber subscribes to the event, and when the RaiseEvent() function is called, the HandleEvent method is automatically invoked.
Program-based event access modifier concept.
Program events created in event handlers can be defined as public or private, and C# users can specify the access level of these event accessors (add/remove methods).
public event EventHandler OnClick; // Here we define a Public class event
private event EventHandler OnNotify; // Here we define a Private class event
A detailed summary of event handling in C#.
- Events in C# programs are used to notify other parts of the program when something, such as an event or action, is performed by the system. For example, a button click or a change in system or application state, etc.
- In event handling, a delegate is used to indicate the method signature that will handle the event in the current program. In this, a publisher initiates the event, and a created subscriber listens for the event and responds accordingly within the program.
- Events are typically defined as multicast. Multiple subscribers can be created, and when an event is triggered in the system or application, all subscribers are notified.
