Author avatar

Dániel Szabó

Handling What Needs to Be Handled

Dániel Szabó

  • Jul 26, 2019
  • 3 Min read
  • 15 Views
  • Jul 26, 2019
  • 3 Min read
  • 15 Views
Languages Frameworks and Tools
C#

Introduction

In programming, the events are something that are very likely to happen, or can possibly happen. In real life, if we want to put it in context, when we drink the last bottle of milk then we have our insides telling us that we need to buy milk if we want to drink the next time. If we ate the last slice of bread, we need to go to the bakery and buy some more. Where is the line? How often do we need to buy these things that were consumed if there is no more left of it? How do we optimize the process of replenishing our supplies? Do we take a note and go shopping every Friday afternoon? Do we precalculate the amount of food we need for the next week and try to buffer some more in case something comes up?

These real life problems describe what events are and how we make sure that we always have enough milk and bread between two shopping trips.

Delegates

In order to proceed further with our journey, we need to understand the concept of Delegates.

Simply put, a delegate is a pointer or a reference to a method. This approach allows us to pass them around like values. The delegates mystery can be easily debunked, the most important feature or concept of it is the signature (or shape).

The signature consists of two important parts:

  1. return value
  2. input arguments

The looks of a delegate that takes a string as an argument and can be pointed to methods that return void.

1
public delegate void MissingConsumableHandler(string name);
csharp

This event will cause any method which fits the signature of MissingConsumableHandler to be executed.

1
public event MissingConsumableHandler OutOfMilk;
csharp

Events

Now that we know how to create a delegate, we need to justify the existence of the events. We would like to cause a chain-reaction to take place when we run out of milk.

Let's create something to be called upon when this event is fired.

1
2
3
4
void HandleOutOfMilk(string name){
	Console.WriteLine($"Adding {i} to the shopping list!");
	Console.WriteLine($"Notifying Mom about the disaster!");	
}
csharp

The mortar between the event and the methods that we expect to be executed are the delegates. The given event must internally store a list of pointers to the methods we would like to call when the event is raised. In order to do this, we need to know the arguments to pass to it!

Simply put, a delegate is a contract between the list of methods and the event upon which they are called.

This is how you glue them together:

1
Freezer.HandleOutOfMilk += new MissingConsumableHandler(HandleOutOfMilk)
csharp

In order to raise this event, we only need to invoke the following code:

1
HandleOutOfMilk("3 bottles of milk")
csharp

Conclusion

That is all there is to it. My goal was to peel away the magic behind the topic. The events under the cover are nothing more than a list of methods with the same shape (signature). The list lives in symbiosis with the event; they are stored with the event. When the event is raised, all that happens is that the methods are iterated over and called with specific arguments. The assignment of an event handler is just eye-candy or a prettier way of adding a list of methods to be called. This demo can be further extended to call an external API for a shopping application, or even setup to a smart-refrigerator to automagically handle the stuff for us.

0