Delegate: A Person Sent or Authorized To Represent Others, in Particular An Elected. Representative Sent To A Conference
Delegates allow methods to be passed as parameters and invoked remotely. A delegate acts as a representative for a method. When an event occurs that matches a delegate, any methods associated with that delegate through an event are invoked. In the example, a House class has a static FireAlarm event to notify others when the house burns. A Fireman and Doctor subscribe to the event using delegates. When the house burns, the FireAlarm is raised, invoking the Fireman and Doctor's delegate methods to handle the fire.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
72 views6 pages
Delegate: A Person Sent or Authorized To Represent Others, in Particular An Elected. Representative Sent To A Conference
Delegates allow methods to be passed as parameters and invoked remotely. A delegate acts as a representative for a method. When an event occurs that matches a delegate, any methods associated with that delegate through an event are invoked. In the example, a House class has a static FireAlarm event to notify others when the house burns. A Fireman and Doctor subscribe to the event using delegates. When the house burns, the FireAlarm is raised, invoking the Fireman and Doctor's delegate methods to handle the fire.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6
Today Im talking about events & delegates,
that unknown partner for a lot of you.
Google English Dictionary explains the meaning of delegate: Delegate: a person sent or authorized to represent others, in particular an elected. Representative sent to a conference. I want to single out "sent or authorized to represent others". So a delegate in C# programming is an entity authorized to represent others and it can be sent for this purpose. This means that we have something that needs to be represented. What is it? Well in our case, it is a Method (Function). The method resides somewhere. It may belong to an object or it maybe just a static method. If the method needs to have a remote representation, it uses a delegate. The delegate can be sent someplace far from the represented method but if need be, the delegate can easily invoke the method no matter how far apart they are. So why was the entity called delegate introduced? It was mainly done to be able to invoke any method remotely. It also allows us to somehow pass the method as a parameter. . Basic usage A basic usage of event & delegates should look like this: //Delegate declaration public delegate void CallBack(); //CallBack event declaration public event CallBack MyEvent; private void RaiseMyEvent() { if (MyEvent != null) MyEvent(); } Delegates are descriptions (like prototypes in C) about how a method looks like, and we can use those descriptions as variables in order to have dynamic methods inside our classes. That means that we dont know who implement it, instead of that, the implementation is given by someone that will say I am who implement this method. Some Theory By default, delegates in C# are multicast, that means that more than one method can say I am who implement this method and all of them will do the work at the same time, so in order to give this a little sense there exists events which give the semantical meaning to a delegate, while a delegate is a method, an event is something that will be launched when something happens or determinate conditions have been accomplished in our class Note in the code above that we also made a method declaration private void RaiseMyEvent() Thats because we cant call a dynamic method that have not been set, so we will need to check if that description have been provided by someone, I highly recommend you to use the code above as the minimum code needed for an event/delegate declaration The example I created an example so you can see what happens when we do get this to a real example :). Imagine a House that gets on fire and there exist a Fireman and a Doctor that care about it. Here is the House implementation, It has a fire alarm for when its on fire public class House { //Delegate declaration public delegate void FireAlarmDelegate(); //FireAlarm event declaration public static event FireAlarmDelegate FireAlarm; private static void RaiseFireAlarm() { if (FireAlarm != null) FireAlarm(); } public static void Burn() { Debug.WriteLine("Burn!!"); RaiseFireAlarm(); } } Here is the Fireman, it cares about when the fire alarm rises up and do what is needed in that case. public class Fireman { public Fireman() { // I am the FIREMAN and I want to be // notified when the house is on fire. House.FireAlarm += HouseIsOnFire; } private void HouseIsOnFire() { Debug.WriteLine("I am the FIREMAN"); } } Here is the Doctor, and it also cares about when the fire alarm rises up and do what is needed in that case. public class Doctor { public Doctor() { // I am the DOCTOR and I want to be // notified when the house is on fire. House.FireAlarm += HouseIsOnFire; } private void HouseIsOnFire() { Debug.WriteLine("I am the DOCTOR"); } } When we create the fireman he start taking care about the house fire alarm (the same with the doctor), so when we burn the house, they will do what is needed in their respective methods. If we get this into practice: Fireman fireman = new Fireman(); Doctor doctor = new Doctor(); House.Burn(); The result looks like this: - Burn!! - I am the FIREMAN - I am the DOCTOR