In .Net we often listen to the word “Delegate” and whenever asked about the same in interviews or somewhere else, there is a short and sweet answer-“The delegate is a pointer to a method.”
The answer is simple, concise and great but what next. It is pointing to a method but why. Why do I need to point a method and when I can call it? Furthermore, it is ok that it’s pointing something, but how to use it in an application scenario. Let’s find out.
Let’s start with a simple meaning of word “Delegate”. So when you search for this word in Google (not with c#) you find a definition something like following:
“A person sent or authorized to represent others, in particular an elected representative sent to a conference.”
So if we leave the elected or conference part, and take a look at the first part which has the whole meaning of the delegate-“A person sent or authorized to represent others”. What do we understand with that?
The definition tells us that the delegate is somebody who is authorized to do some work on behalf of someone who is sending him (as a delegate to other party.) And let me tell you, this is exactly the meaning of delegate in c#. So now let’s comeback to the famous (and too much compressed or zipped or a bird eye view type) answer of delegate question that it is a function pointer. So now it feels right. Now we can say that the delegate points to some function (on behalf of some function) and may get a call back if required or provides a level of abstraction between two parties so the two parties are not dependent on each when they get update their functions.
Let us understand with an example:
Pic-1: What delegate does and how is this a pointer to a method.<br/>
The above pic describes one of the functions of a delegate-abstraction or decoupling. Remember, this decoupling can be achieved by Interfaces also, but there are different scenarios for each of them. You can’t say that this is a concrete scenario where I will use delegate or that is the scenario where I will use Interfaces.
I think every person knows about “Events” and have heard about delegates and events together. So when events come into picture, there is a model followed which is called “Publisher/Subscriber” model and that uses delegates as its de facto mechanism otherwise that’s up to you and some guidelines and architecture followed in the solution. If you want more discussion over it take a look at following link:
https://msdn.microsoft.com/en-us/library/ms173173.aspx
The picture above makes you understand that the delegate can be some kind of variable which takes the responsibilities of pointing to functions and then we can achieve some decoupling through it but what else can we achieve through delegates. Well, I have a list below:
1-Abstraction and Encapsulation:
We define an abstract delegate which points to method. That way we don’t need to expose a method directly to another class.
2-Callbacks:
Delegates are passed to a method and that method can use the delegates for callbacks or passing some information back to the calling method.
3-Asynchronous Processes:
We can call delegates “Asynchronously” or in the background using begin invoke or end invoke method.
4-Multicasting or Sequential Processing:
We can call methods in sequence using multicasting of delegates.
5-Using Events:
We can use events by using delegates.
Now that we know an intro about delegate along with its applications, we can easily relate that to a simple c# example and in next part we’ll see about that.
In My Next blog I will explain you about: Delegates-How do we use them in C#?
Happy Coding. :-)
0 Comment(s)