Hi friends,
In today’s discussion i would like to discuss more about delegates. This is one of the topics which i struggled most, hence thought to talk about. But, when i checked the topic back again, then understood it’s importance and came to know, it’s not that tough.
so, let’s get started.so, before jumping to the demos, let’s jump into the basic lessons of delegates and it’s role in real world.
Delegates:- Frankly speaking delegates are basically glue between event raiser and event handler. Delgates are the one which is making everything possible, when it comes to raising an event and handling an event. so, basically if i raise an event, then how does it will get passed on to the event handler, then in order to bring this guy event raiser to event handler, delegates play a key role here.
Events:- Events are basically important or key part as far as delegates and it’s role is concerned. it could be simple click or index change event in dropdown or we might even have custom object like an order object like when a customer places order and that notification goes out and notifies the other part of the system. so, basically an event is a notification which is getting passed on to the event handler. also, it provides a way to trigger notifications. so, basically this message goes out to one or more subscriber who has subscribed for the event.
Role of Delegates:- Delegates role are very important, they bring event data from one point to another point means it carries data from event raiser to event handler. so, delegates are often called as function pointer. now, actually when we create delegate and when we use events and when we are creating delegates, so basically we are doing to couple of things. there is a special class in .net called Multicast delegate. and this is the class which tracks everyone that’s listening. so, when the event notification goes off with some information like OrderId = 1, then this information needs to be sent to all the listeners. so, if there are 100 listeners, then we would have 100 objects inside of invocation list.
so, the reason why we call delegates a function pointer,it’s because of the reason of event handlers because we need to point the data through the pipeline over to eventhandler.
Role of Eventhandlers:- so, the final thing is event handler. so, when the event raiser raises the data and then delegates route it to event handler. then event handler process the data, may be it updates the UI or database. so, it’s basically responsible for receiving data from delegates and then processing the same. It normally receives two parameters
1) Sender and
2) EventArgs. Event Args are basically event data.
//Creation of delegates:-
so when it comes to creating delegates, it’s actually very simple process. so, actually we have to define basically blueprint for eventhandler. so,
a custom delegate is defined by using the word delegate. Now, this is kind of magic keyword, now behind the scenes when the compiler sees the delegate keyword,
it actually generates a class that inherits from another .net classes. so, basically a delegate is a blueprint for the method alongwith data which is going to be passed on to the handler.
Delegate has two really important property
1) method and
2) target property.
so, the method is that pipeline has to dump the data somewhere and you have to define the name of the method, where that data should go. Now, the target would be if you have to have an object instance where that method lives, then target would be actual object that has the method.
3) GetInvocationList():- Now, there is a class called multicast delegate that’s built into the framework as well. so, every delegate we create once compiled will inherit from multicast delegate. so, multicast delegate is really a way to hold multiple delegates. in other words, i may have one message to send out, but i have to send it across multiple pipelines. it’s not like that we are directly inheriting from delegate or multicast delegate rather the way we do is we use delegate keyword.
so, multicast delegate is just an array of multiple pipelines or multiple delegates. and it happens synchronously kind of top of the list.
//Creating Delegate –> Defn of delegate
public delegate void OrderPlacedHandler(Int16 Hours, OrderType orderType);
Now, when i run the same , it will work properly and give the output as shown below
so, in the above example basically i have hardcoded the call and then invoked, but let’s consider a scenario where in it is really very useful, it could be
dynamic call and from there we’ll be using the same like shown below
so, with this i would like to wrap this up. Till then stay tuned and happy coding.
Thanks,
Rahul