Saturday, December 05, 2020 05:35

Table of contents >> Delegates, Lambda Expressions, Events > Events Add and Remove

Events Add and Remove

In the previous lesson I showed you how the compiler actually implements events by actually adding two methods named addon() and removeon() in the background and making the Action field private, so we can’t invoke it. But that was done in MSIL language, and we really don’t need to deal with such a low level. In this lesson, I will show you how we can do the exact same thing using the usual C# language, in case we need more control over how our events are implemented.

Let’s take again the code that we had in the previous lesson:

And now, let’s re-trace all the steps that the compiler did automatically – first, let’s make our Action private, so it can’t be invoked directly:

I’ve also changed the name of the Action to start with a lowercase character, just so we won’t have a naming conflict later on.

Step two – add a public event:

You will certainly notice the new way of declaring my delegate: suddenly, my event declaration has a body, and inside it, I am adding two constructs, add and remove. This is the C# way of allowing us to do just the same thing as addon() and removeon() were doing automatically. It is in these constructs that we can subscribe and unsubscribe to our private Action, by using a ghost parameter named value (also present when declaring a property). To test this, let’s test that these constructs are executed when something subscribes or unsubscribes to our events:

The explanation is simple: we are subscribing a method to a private Action by using the add and remove event constructs, which is the exact same thing that the compiler does automatically, if we don’t specify add and remove blocks. But, by specifying them, we have a greater control, because we can suddenly implement logic when adding or removing to an event, like, for example, checking if the subscriber method meets some requirements, or logging the actions, or subscribing the method more than once, or whatever.

The result of the above code will look like this:

C# events add removeAt this point, you can kind of start to see that the event is just an interface towards our private Action, so that we are in control of who and when invokes that Action, and by using the add and remove event members, we can also control who can subscribe and unsubscribe to the Action, when it can do so, under what conditions, etc. This really gives us all the control we could want when it comes to securely playing with delegates.

True, most of the times, you won’t need this degree of control, using an event in a simple manner, like in the previous lesson, is more than enough for most daily life cases.

Comments

comments

Tags: , , , , , ,

Leave a Reply



Do NOT follow this link or you will be banned from the site!