0
0
C Sharp (C#)programming~30 mins

Custom event arguments in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
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
1
Create the custom event argument class
Create a public class called TemperatureChangedEventArgs that inherits from EventArgs. Add a public property Temperature of type double with a getter and a constructor that sets this property.
C Sharp (C#)
Need a hint?

Remember to inherit from EventArgs and create a constructor that sets the Temperature property.

2
Create the TemperatureSensor class with an event
Create a public class called TemperatureSensor. Inside it, declare a public event called TemperatureChanged of type EventHandler<TemperatureChangedEventArgs>. Also, add a private field _temperature of type double.
C Sharp (C#)
Need a hint?

Declare the event using EventHandler<TemperatureChangedEventArgs> and a private field to store temperature.

3
Add SetTemperature method to raise the event
In the TemperatureSensor class, add a public method called SetTemperature that takes a double parameter called temperature. Inside this method, set the private field _temperature to the new value, then raise the TemperatureChanged event if it is not null, passing this and a new TemperatureChangedEventArgs with the new temperature.
C Sharp (C#)
Need a hint?

Use the null-conditional operator ?. to safely raise the event.

4
Subscribe to the event and print the temperature
Add a Main method inside a Program class. Inside Main, create a new TemperatureSensor object called sensor. Subscribe to the TemperatureChanged event with a lambda that takes sender and e and prints "Temperature changed to {e.Temperature}". Then call sensor.SetTemperature(25.5).
C Sharp (C#)
Need a hint?

Subscribe to the event using += and a lambda expression. Use Console.WriteLine to print the temperature.