Member-only story

Introduction to .NET Events and Event- Handlers

Jawadhasan
4 min readNov 17, 2020

--

Introduction

Our physical world is surrounded by events e.g. Door Opened, Engine Started, Plane landed etc and we see events in the virtual world as well e.g. email received, friend-request sent, post liked etc. This is a very common concept, many programming languages support this and in this post we will see that how we can use events in .NET applications.

Events are part of .NET Framework and can be used for notification purposes inside your code. I will suggest you to use this functionality as it will reduce coupling in your code.

There are a lot of articles about this functionality and sometimes those contain information about underlying infrastructure code e.g. delegates, multicast delegates, invocation list etc. Those are important concepts but if you are doing some basic notification, then you even don’t need to know about those details and this is what I am going to show you in this post.

The examples are very simple and that is the idea, keep this post simple and same time learn how we can use this built-in notification system in our code.

Defining an Event

Events can be defined in a class using event keyword.

public class Printer { 
// event [delegate] [eventName]
public event EventHandler printed;
}

Here we are using built-in delegate (EventHandler) in the above example. So printed is an event, which this class will…

--

--

Jawadhasan
Jawadhasan

Written by Jawadhasan

Software Solutions Team Lead | Cloud Architect | Solutions Architect

No responses yet