Events in C#: When and How to Implement Them

Events in C#: When and How to Implement Them
December 18, 2023
8 minutes read

Welcome! You’ve probably been hearing a lot about C# events, but you’re wondering, How does it all tie together? Just sit back, because in this article, you are going to catch up on all the details about C# events and their application.

It’s like learning to play an instrument; before you start composing, you need to understand how to read the musical notes. Before diving into the advanced stuff, let’s understand the basics. Stick with me, and I promise, you’ll be writing captivating symphonies – I mean, coding in C# in no time!

Introduction to C# Events

Hang on, what really is a C# event? Covert operations? Nope! In the world of C#, an event is simply a way that a class can notify other classes or objects when something of interest happens.

Overview of Events in C#

Events in the context of C#. Imagine you’re at a concert. The band playing is the publisher of events (C# class), and the audience (other classes or objects) is waiting for something noteworthy (an event) to happen. When the band starts playing (an event occurs), the audience reacts (responds to the event). In programming terms, events in C# provide notification when a specific action has occurred in an object.

Loading code snippet...

In this code snippet, the AlarmClock class publishes the AlarmSounded event, which the Person class listens to. When the AlarmSounded event is fired after 5 seconds, the person’s wake-up method responds.

Role of Event Handlers in C#

In a festival, event organizers handle everything, right? Similarly, in C#, event handlers handle the response to an event.

What is an Event Handler in C#

In C# event handling, an “event handler” is a delegate that gets called when a given event is fired. Like a listener who is waiting for their favorite song to play, the event handler is waiting for the right event to take action.

Loading code snippet...

In the example above, the User class has a method PerformAction that serves as an event handler. Whenever the Button is pressed (event happens), the user performs a particular action.

Implementing Events in C#

Ah! The practical aspect of any programming language, that’s where the real fun begins, isn’t it? It’s time to swing into action and see how we can implement events in C#. Let’s roll up our sleeves and dive into the coding part!

Step-by-Step Guide to Implementing Events in C#

Implementing events in C# is like taking a step-by-step journey of orchestrating when and how certain actions are responded to in your program. Remember, it’s a journey best taken one step at a time. Let’s break down the process:

  • Define a delegate that matches the signature of the event handler method you want to use.
  • Declare an event using the delegate you defined.
  • Implement a method, within the provider class, that raises the event.
  • Create a method in the subscriber class that will handle the event (conforms to the delegate).
  • Subscribe the event handling method to the event.

Sounds like a lot? Let’s simplify this with a real-world example.

Real-World C# Event Example

How about we consider a traffic light system? The lights changing (events) cause cars to react (event handling) accordingly. Here is a simplified version of how this could be coded:

Loading code snippet...

In the code above, when the traffic light color changes, the event TrafficLightChanged is fired and causes the car to react. If the light is green, the car moves; if it’s red, the car stops.

Let’s return to the email sending example. In addition to sending the email, we may also need to acknowledge the recipient that the email is being sent.

Loading code snippet...

In the newly introduced code snippet, when the Acknowledge method in the EmailAcknowledge class is called, it raises the event AcknowledgeSending, which the Recipient class is listening to. As soon as this event is raised, the OnAcknowledge method in Recipient class gets called and the name of the recipient is printed.

Working with EventArgs in C#

Whew! You’ve made it far. Next stop is EventArgs town. Ready?

Understanding C# Event Args

In C#, ‘EventArgs’ is a class that provides data related to an event. Think of EventArgs as the courier carrying the message that an event has occurred.

Loading code snippet...

In this example, SongPlayedEventArgs carries the name of the song that’s been played. Listener uses this custom data (PlayedSong) to respond to the SongPlayed event.

Advanced Concepts in C# Event-Handling

Take a deep breath, grab a coffee if needed, because we’re about to delve deeper into the abyss of C# events. Just like building a towering Lego skyscraper, it’s all about understanding the intricacies and techniques to assemble the blocks effectively.

Safe Event Invocation

One of the essential aspects of event handling in C# is to always ensure safe event invocation. Listener classes might unsubscribe to an event at any given point in time. Invoking an event without any subscribed listeners could lead to a null reference exception – and trust me, we don’t like those, do we?

Here’s how to safely invoke events:

Loading code snippet...

The ?. syntax is C#’s shortcut for null checking before invoking the event handlers, saving us from possible null reference exceptions. It’s as though you’re checking if the coffee machine is indeed present before pressing the brew button.

Passing Data Using Custom EventArgs

In many scenarios, we need to pass some data along with our events. That’s where custom EventArgs come into the picture.

Let’s suppose you’re building an app for a library which fires an event whenever a new book arrives.

Loading code snippet...

Here, we created a NewBookEventArgs class that inherits from EventArgs and added two properties: Title and Author. We fire the event by creating a new instance of NewBookEventArgs and passing it along with the event invocation.

This way, event handlers (like OnNewBookArrived from the Member class) can access this data and act accordingly.

Unsubscribing from Events

Here’s the thing: every time a class subscribes to an event, it forms a strong reference with the event source. This reference can stop the garbage collector from reclaiming the memory, leading to potential memory leaks (aka, the silent resource murderers!).

Just as you would turn off the lights before exiting a room or unsubscribe from an annoying newsletter in your inbox, remember to unsubscribe from events when they are no longer necessary.

Loading code snippet...

The Register and Unregister methods handle the subscription and unsubscription from the publisher.OnPublish event. Simple enough, right? Yes, it’s like you’re giving your consent to receive those marketing emails. And when you decide you’ve had enough? Simply unsubscribe!

Conclusion

Events and the Future of C# Programming

Wow, quite a journey, right? Have events become your new best friends? They should, as encapsulation of behaviours into events streamlines your code, makes it cleaner, and more efficient. It might seem like a challenge now, but the more you meditate on this knowledge, the easier it will be to grasp its essence.

So, go ahead, experiment with some code. Remember, in programming, just as in life, practice makes perfect! Happy coding, folks!

You May Also Like

Optional Parameters in C#: What You Need to Know

Optional Parameters in C#: What You Need to Know

Programming in C# often requires flexibility and simplicity ...

What is Lock Keyword in C#? Main Usages

What is Lock Keyword in C#? Main Usages

IndexUnderstanding the C# Lock Keyword Can’t tell a lock fro...

Enumerate in C#: Detailed Explanation

Enumerate in C#: Detailed Explanation

Desperate to decode the mystery that is enumeration in C#? I...

Creating a JSON Class in C#: Detailed Guide

Creating a JSON Class in C#: Detailed Guide

In the world of application development, manipulation of dat...

Static Class in C#: How to Use It?

Static Class in C#: How to Use It?

Hello there, future C# aficionado! It’s time to roll down th...

DateTime Formatting in C#: Dev Guide

DateTime Formatting in C#: Dev Guide

Have you ever gotten frustrated handling dates and times in ...

Leave a reply

Loading comment form...