Understanding delegates, events and event handler.
This is the first post in a series of posts focussing on event, delegates and event handlers. In the series we will discuss what each and what its role in event driven programming. The first post will be theory where we bring into focus what each is and we will try to explain using a simple example.
In this post we are going to look at delegates, events and event handler and understand how each of them depend on the other to work. A delegate in C# is more like a function pointer in C++. By function pointer, means that, it means that the delegate points to a function , and so it will know which function to call at runtime. Basically, a delegate is a specialized .NET class. So at runtime, a class is generated for every delagate you declare. This generated class inherit from MultiCastDelegate class which tracks the subscribers.
Delegates, plays a major role in event driven programming, as it acts as the glue/pipeline through which the event sends or communicates with the event handler. Its through the delegates that you pass the events arguements to event handlers.
Event arguments encapsulate the actual data from the event to the event handler. For example, in windows application, we have useful event like mouse click, which passes lot of data including x and y co-ordinates of the clicked point. We have others like button click event which has minimal data to pass.
Event Handler, is a method that gets data from a delegate, and process it appropriately. The difference with other methods is that its signature is dictated by the delegates itself. But most of the event handlers and delegates takes two parameter, sender and EventArgs. The sender represents whoever raised the event or who owns the event. e.g OnClick, the sender is the button as its the one to be clicked. EventsArgs is extra data which the handler of the event might find useful.
Event are a way of notification and are used every in .net framework. They provide a way to trigger notification and alert from the interaction of the program either by a user or other programs. In short they signal an occurence of an action. Once an event is raised, it passes data (EventArgs) which is very important to the method responding to it. And so between an event and event handler there is a delegate, otherwise you are just calling a function just like any other. You can raise an event in two ways
- calling the event like methods
- access the events delegate and invoke it directly
The best way to explain is a hospital set up, we have a doctor, patient and the receptionist. Once a new patient arrives he provides information to a receptionist. We can have more than one receptionist, doctor and patient as well. The receptionist takes paperwork containing patient information as well as his (Receptionist) name so that the doctor can know who attended the patient. The hospital protocal is that a patient should not go straight to the doctor. So medium to pass information to the doctor is the receptionist. With reference to the explanation we have so far we can deduce the following:
-Patient: Is the event raiser/he will own the event.
-Doctor: Event handler. He knows how to treat the patient
-Receptionisht: Delegate. He knows the doctor who will treat the patient given a patient.
-Patient information: Event args.
We can define our event like PatientArrived. So when the doctor arrives in the hospital he prepares himself and alerts the receptionist that he can now attend to patient who arrives. So this means he can handle PatientArrived event.
Hope this makes it easier to understand the concept behind Events, Delegates and Event handler.
In the next blog we will now present this information using code and you will see how each will come into play.
Happy coding 🙂