3 extensibility techniques using delegates, multicast delegates and C # events (CSharp) – Programming, Pseudocode Example, C# Programming Example
C#

3 extensibility techniques using delegates, multicast delegates and C # events (CSharp)

The first techniques mentioned

Usually, when I ask developers (in my trainings) how they implement extensibility mechanisms, I often have answers about class inheritance and method overload.
These two mechanisms are of course possible, but they are not always the simplest and most effective to use.

The use of inheritance or overload will especially complicate your code. The next developer who will have to read your code will have more difficulty understanding and reusing what you have written.

In the beginning, I also used the inheritance (a little too much besides :-)) and the overloads of methods. From now on, I also use other techniques that make it possible to do things differently and better (in my opinion).

I spoke of the principle in the previous article on delegates in csharp. So I’ll quickly summarize what is possible here and extend the mechanism to events.

The principle

Delegates are references to methods in C #. The idea is to use these references as an extensibility mechanism.

Providing an extensibility mechanism in your code will allow an existing behavior to evolve without changing the code that performs this behavior.
The idea behind all this is simple: once a piece of code tested and validated, we must try not to touch it.
To add a new behavior, it will be possible to use the delegates in C # or the events.

Extensibility thanks to delegates in CSharp

Delegates allow references to “external” methods that can be written later.
So, if you write a method, you can call delegates to perform operations in your code.

The most common use of delegates is to be able to choose between one algorithm and another (design pattern Strategy).

I suggest you discover an example that will allow you to understand. At first, I start by writing a basic algorithm (in the example, it adds a random number to each element of an array of integers).

By adding a delegate as a parameter, I can propose a behavior that allows to add a parameterizable filter (which joins the Strategy pattern pattern).

Here is the corresponding code:

Scalability thanks to multicast delegates in CSharp
Multicast delegates are a special type of delegates. In fact, it is possible to assign more than one method reference to a single delegate.
This will allow you to call several methods when you call your delegate.
The cool thing is that a classic delegate can also be used as a multicast delegate without changing the code of your algorithm.

To add multiple methods to a delegate, just use the “+” operator. To delete a method, the operator “-“.

I propose you to adapt the previous code to be able to use several filters on the numbers in input. It will simply be necessary to pay attention to the order of the calls (the order of addition of the delegates).

Here is the code adapted:

 

There is a little subtlety so I have to talk to you: I used the keyword “ref” in the delegate.
This is necessary so that each delegate can “work” on the same set of data.

This mechanism makes it possible to call the Algorithm method with two filters:

The first SimpleFilter filter to delete items whose value is 1,
The second SecondFilter filter inverts the order of the elements.

Extensibility with events

Finally, the third method I wanted to talk about uses events in C # (key word event).

Events in C # allow to set up “recall” mechanisms at the level of the class. The classes you write can warn subscribers that something has happened.
event is a keyword that allows to declare a property of the type delegate multicast (this also allows events to be used interfaces).

In the .NET framework, events are used everywhere (especially in graphical controls).
For example, on the Button class, you have a “Click” event. This will be launched when a user clicks a button.
This mechanism will call several methods to make a treatment.

There is a little subtlety with events: only the class that declares the events can launch its events.
It will therefore write code in your class to launch the appropriate events.

Here is an adaptation of the previous algorithm example (I put the set in a class after a bit of refactoring).

We therefore note that the events make it possible to set up extension mechanisms at the level of the class whereas the delegates are used to extend methods.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.