What are Delegates and Events

First of all we will discuss what is delegates, what is the meaning of delegates in real world. Delegates means pointer to a function.

It’s mainly used for call backs. Now it will comes to our mind what is CallBacks?

Let’s take a real life example, when we call a person, if he/she doesn’t receive our phone, after sometimes he/she will callback us. Actually that’s called CallBack. Just like in our application we want to read a directory and also want all file namee in every file read. Generally, in each reading file we want to callback this filename.

Delegates are two-way or bidirectional.

Example: Chating with someone is called bidirectional.

delegates

In above image we want class Y return something to class X in each line of execution. That’s why we send a delegate from class X to Class Y and that delegates return something to Class X. Now we can communicate between Class X and Class Y, That’s why delegates are called Bidirectional.

Now we will discuss about events.

Events means Publisher and subscriber. It means when we publish something we want to show this publication to somebody who subscribe.

Example: Live new feeds.

class

In above diagram, Class Y and Class Z subscribe once from class X and Class X always sends all update to Class Y and Class Z.

Let Class X is Cricket news channel, Class Y and Class Z are two users, Class Y and Z wants news from Class X. First, Class Y and Class Z wants to subscribe that news channel (class x), now class Y and Class Z can access the news of Class X.

Points to remember:

Events are encapsulated over delegates.

Summary:

  Delegates Events
Used For Call Back. Publisher and Subscriber
Communication Two Way Communication. One Way Communication.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now