C# EventHandler
By FoxLearn 1/18/2025 1:53:04 AM 96
- 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.
- Using the OrderBy and OrderByDescending in LINQ
- Querying with LINQ
- Optimizing Performance with Compiled Queries in LINQ
- MinBy() and MaxBy() Extension Methods in .NET
- SortBy, FilterBy, and CombineBy in NET 9
- Exploring Hybrid Caching in .NET 9.0
- Using Entity Framework with IDbContext in .NET 9.0
- Primitive types in C#