Custom Event Arguments in C#
📖 Scenario: You are building a simple notification system where a TemperatureSensor reports temperature changes. You want to send extra information about the temperature when it changes.
🎯 Goal: Create a custom event argument class to hold the temperature value, raise an event with this custom argument, and handle the event to display the temperature.
📋 What You'll Learn
Create a class called
TemperatureChangedEventArgs that inherits from EventArgs and has a property Temperature of type double.Create a class called
TemperatureSensor with an event TemperatureChanged using EventHandler<TemperatureChangedEventArgs>.Add a method
SetTemperature in TemperatureSensor that raises the TemperatureChanged event with the new temperature.Create a
Main method that creates a TemperatureSensor, subscribes to TemperatureChanged, and prints the temperature when the event is raised.💡 Why This Matters
🌍 Real World
Custom event arguments are used in real applications to send extra information when something important happens, like sensor updates or user actions.
💼 Career
Understanding custom events is important for building interactive and responsive applications in C# and many other programming environments.
Progress0 / 4 steps