Event and Delegate in C#
In this post, I will talk about two important concepts in C#, which are Event, and Delegate.
What is an Event and why do we use an event?
In C#, an event is a mechanism for communication between objects. That means when something happens in an object, any objects listening to that object would be notified about that event. In java, we have already seen the concept of event, especially with graphical user interface. One of the example is when a user click a button, the class that represent control in that interface is notified.
Events are not only used in graphical user interfaces; events are used for providing ways to objects to notify stat changes. Another example that we saw in java is the Observer Observable pattern, where observers subscribe to the observable in order to get notified whenever a change happens.
In event communication, the sender class does not know which object or method will receive the events it raises. Here is where a delegate plays a role, which is an intermediary between the source and the receiver, so a delegate is a type of events. It helps in building loosely coupled applications, which means components or classes are not tightly coupled together. Loosely coupled applications are easy to extend without breaking or changing one or more of existing capabilities.
How this works with a code?

This is a VideoEncoder class with one method Encode, which encode a video passed to it. For simplification, the encoding logic is not implemented since it is not necessary for understanding this case study.
When the encoding is finished, an email is sent to the owner of the video by the mail service class. So far, everything is fine with this code, but in the future, let say we want to send a text message to the owner of the video as shown below.

Now since we have added the last line of code, the Encode method has changed, which means the Encode method has to be recompiled, the VideoEncoder has to be recompiled, and any other classes that depend on the VideoEncoder class have to be recompiled and redeployed. This is not a good practice for developing applications. We want to design applications such that if we change something, that change has a minimal impact on the overall applications.
To solve this problem, we can use events, where the VideoEncoder publish an event and the mail service subscribe to that event. In this case VideoEncoder knows nothing about mail service. That means, in the future, we can add another service such as a text message service and have it subscribe to the video Encoder event.
To send a message to the subscribers, video encoder, which is the publisher, will have to invoke a method in the subscriber. This is where the delegate comes in. A delegate is a contract between the publisher and the subscriber. It determine the signature of the event handler in the subscriber.
Now let see how this works.
To implement an event using delegate we will follow three steps.
· Define a delegate
· Define an event based on that delegate
· Raise the event
The code below shows how to implement a delegate including the tree steps above.

Note: by convention the method to raise the event should be protected virtual, and the naming should start by “On” followed by the name of the event. In this case the event is “videoEncoded”, and the method to raise this event is “OnVideoEncoded”.
Now let create two subscribers MailService and MessageService (as shown below)


Now, we have the publisher and two subscribers. Let see how to use a delegate. The code below shows how mail service and message service subscribe to the publisher video encoder.
We can see that, if we need to add another service, we can just subscribe to the publisher, and we do not need to change the publisher since the publisher knows nothing about subscribers.

Note: as you can see in this screenshot, the method OnvideoEncod() does not take parenthesis.
This brings us to the end of this post. I hope that you have now a clear understanding on how to to use event and delegate in your code. If you have questions about event and delegate or any questions on my previous post, feel free to ask me.
Thank you for reading to my post.
