C# EventHandler
By FoxLearn 1/18/2025 1:53:04 AM 41
- A publisher that notifies subscribers when an event occurs.
- One or more subscribers that listen for events and react accordingly.
C# - Example of using events
Let’s go through three simple steps to use events in C# with a different example:
Add an event field
The first step is to define an event field in a class using the event
keyword and a delegate type (for instance, EventHandler<T>
). Below is an example that shows how to declare an event in a class:
public class TemperatureSensor { public event EventHandler<int> TemperatureChanged; }
Here, the TemperatureChanged
event uses EventHandler<int>
, meaning it will send an integer value (temperature reading) to any subscribed event handlers when the event is triggered.
In this example, TemperatureSensor
is the publisher in the observer pattern.
Delegate Types
While you can define custom delegate types, C# provides built-in ones to simplify the process:
- EventHandler<T>: Use when sending event data of type
T
to subscribers. - EventHandler: Use when you just want to notify listeners without sending any data.
For example, EventHandler<T>
has the following signature:
public delegate void EventHandler<T>(object? sender, T e);
Add an event listener
An event listener is a method that reacts when the event is fired. The listener method’s signature should match the event delegate.
Here’s an example of adding an event listener using a lambda expression for the TemperatureChanged
event:
var sensor = new TemperatureSensor(); // Add event listener sensor.TemperatureChanged += (sender, temp) => Console.WriteLine($"Temperature changed: {temp}°C");
This is the subscriber in the observer pattern. The lambda expression has the same signature as the EventHandler<int>
delegate (void(object? sender, int temperature)
).
Alternatively, you can add an event listener as a regular method:
var sensor = new TemperatureSensor(); // Add event listener sensor.TemperatureChanged += OnTemperatureChanged; void OnTemperatureChanged(object? sender, int temp) { Console.WriteLine($"Temperature changed: {temp}°C"); }
This is how event listeners were commonly added before C# introduced lambdas.
Invoke the event
To fire the event and notify all subscribers, invoke the event from the class that defines it (the publisher). Here’s how you can trigger the event when the temperature changes:
public class TemperatureSensor { public event EventHandler<int> TemperatureChanged; public void SetTemperature(int newTemperature) { TemperatureChanged?.Invoke(this, newTemperature); } }
It’s important to check if the event is null before invoking it to avoid a NullReferenceException
when no listeners are subscribed. This can be done concisely using the null conditional operator (?.
).
Now, when you call the SetTemperature()
method, the event is triggered and all subscribers are notified.
var sensor = new TemperatureSensor(); // Add event listener sensor.TemperatureChanged += (sender, temp) => Console.WriteLine($"Temperature changed: {temp}°C"); // Simulate temperature changes sensor.SetTemperature(22); sensor.SetTemperature(25); sensor.SetTemperature(30);
Output:
Temperature changed: 22°C Temperature changed: 25°C Temperature changed: 30°C
In this example, when SetTemperature()
is called with different values, the TemperatureChanged
event is fired, and the subscribed event listener prints the temperature changes to the console.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#