Event-Driven C#: How to Make Your Code Listen and React

Click follow to turn your code into an attentive listener.

Alex Maher
4 min readFeb 10, 2024

Hey everyone!

If you’re like me, I learn things best when they’re simple to understand and fun at the same time!

When I first learned about delegates, I found it hard to grasp the concept.

So I’ll do my best to make this easy for beginners to understand.

Alright, let’s get going!

Imagine you’re making a program, and you want something to happen when you click a button.

You’re setting up an event (the button click) and telling your program, “Hey, when someone clicks this button, do this thing for me, okay?” That’s event-driven programming in a nutshell.

Here’s how it goes down in C#.

Defining a Delegate

Think of this as creating a custom invitation for a party. It tells your guests (the event handlers) what kind of party it is and what they need to bring.

public delegate void FileDownloadedEventHandler(object source, FileEventArgs args);

Declare an Event

--

--

Alex Maher

.NET C# dev with 10+ yrs exp, self-taught & passionate web developer. Sharing tips & experiences in C# and web dev.