C#: How To Implement Events

Credits to http://www.digitimer.com


I remember the time when my Team Lead asked me to implement an Event type solution for the problem that we have encountered in our previous projects. It was my first time to implement such technique and I was kinda daunted of the said task. In today’s session I’m going to discuss, in the most simplest manner, about raising and consuming events with and without parameters.


What are Events? Events are way for a Class to provide notifications for its clients. Do you remember when we use a button in a Windows form? When we double click the button it will navigate to Button Click which is an event. It is heavily used in GUI related matters but is still very useful for objects to signal if there were any state changes.


The sample application below is just a console app raising and consuming an event without parameters. The app will just show Hello World whenever the DisplayHelloWorld() method is being called. Basically, when the DisplayHelloWorld() method is called it will fire up an event and will call the Display() method which is registered to our HelloWorldEvent event.


So how do we consume and raise an event? First in your class, which in our case is the HelloWorld class, you should declare a property of type event EventHandler. Then we should declare a class that will check if the HelloWorldEvent is empty or not, meaning if somebody already subscribed to our event, and will also be responsible for calling or firing the event which in our case is the OnHelloWorldEvent method. Lastly, we should create a method which, if called, will activate our event which in our case is DisplayHelloWorld().


n our Main class, we should create an instance of the HelloWorld class in order to consume the methods of the class. We need to register the method that we want to be performed when any events happen by using the += operator as shown in the code below. One more thing, if you have already finished subscribing with the event you should unregister it by using the keyword +- operator so that we will not encounter memory leak in our application.


Then to test if our event was successfully fired we run the program and expect the output as shown below:



What if we want to display who said Hello World and the time and date that he/she said it ? In that case we need to setup our HelloWorld class with parameters in our events. I will just name the HelloWorld class to HelloWorldWithArguments class in order for us to see the difference. Let’s check the code below:


As you can see when Display() Method was called we were passing some names to it (sherlock fan btw). So in order to handle that kind of setup we need create another class which will inherit from EventArgs. Let’s call the Class HelloWorldArgument and it should contain the properties that we need to pass to our event.


We need to modify our Display() method to accept one parameter (which is the sender) and create an instance of our newly created class HelloWorldArgument and populate the exposed properties. From there, we will call the OnHelloWorldWithArgumentsMethod which fires our event in the form of the subscriber’s method DisplayWithArguments().



So let’s test the sample app and we should this kind of output:


As we can see from the output, we are already passing parameters to our event. The big difference from both implementation is having to create a different class to handle the Event Arguments needed.


There are more deeper topics or advance techniques about raising and consuming events but at least for now we have established a simple but useful implementation of events both with or without using parameters.

Happy Coding 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s