I understand the purpose of events, especially within the context of creating user interfaces. I think this is the prototype for creating an event:
public void EventName(object sender, EventArgs e);
What do event handlers do, why are they needed, and how do I to create one?
My understanding of the events is;
Delegate:
A variable to hold reference to method / methods to be executed. This makes it possible to pass around methods like a variable.
Steps for creating and calling the event:
The event is an instance of a delegate
Since an event is an instance of a delegate, then we have to first define the delegate.
Assign the method / methods to be executed when the event is fired (Calling the delegate)
Fire the event (Call the delegate)
Example:
Here is a code example which may help:
C# knows two terms,
delegate
andevent
. Let's start with the first one.Delegate
A
delegate
is a reference to a method. Just like you can create a reference to an instance:You can use a delegate to create an reference to a method:
Now that you have this reference to a method, you can call the method via the reference:
But why would you? You can also just call
myFactory.GetInstance()
directly. In this case you can. However, there are many cases to think about where you don't want the rest of the application to have knowledge ofmyFactory
or to callmyFactory.GetInstance()
directly.An obvious one is if you want to be able to replace
myFactory.GetInstance()
intomyOfflineFakeFactory.GetInstance()
from one central place (aka factory method pattern).Factory method pattern
So, if you have a
TheOtherClass
class and it needs to use themyFactory.GetInstance()
, this is how the code will look like without delegates (you'll need to letTheOtherClass
know about the type of yourmyFactory
):If you'd use delegates, you don't have to expose the type of my factory:
Thus, you can give a delegate to some other class to use, without exposing your type to them. The only thing you're exposing is the signature of your method (how many parameters you have and such).
"Signature of my method", where did I hear that before? O yes, interfaces!!! interfaces describe the signature of a whole class. Think of delegates as describing the signature of only one method!
Another large difference between an interface and a delegate, is that when you're writing your class, you don't have to say to C# "this method implements that type of delegate". With interfaces you do need to say "this class implements that type of an interface".
Further, a delegate reference can (with some restrictions, see below) reference multiple methods (called
MulticastDelegate
). This means that when you call the delegate, multiple explicitly-attached methods will be executed. An object reference can always only reference to one object.The restrictions for a
MulticastDelegate
are that the (method/delegate) signature should not have any return value (void
) and the keywordsout
andref
are not used in the signature. Obviously, you can't call two methods that return a number, and expect them to return the same number. Once the signature complies, the delegate is automatically aMulticastDelegate
.Event
Events are just properties (like the get;set; properties to instance fields) which expose subscription to the delegate from other objects. These properties, however don't support get;set;. Instead they support add;remove;
So you can have:
Usage in UI (WinForms)
So, now we know that a delegate is a reference to a method and that we can have an event to let the world know that they can give us their methods to be referenced from our delegate, and we are a UI button, then: we can ask anyone who is interested in whether I was clicked, to register their method with us (via the event we exposed). We can use all those methods that were given to us, and reference them by our delegate. And then, we'll wait and wait.... until a user comes and clicks on that button, then we'll have enough reason to invoke the delegate. And because the delegate references all those methods given to us, all those methods will be invoked. We don't know what those methods do, nor we know which class implements those method. All we do care about is that someone was interested in us being clicked, and gave us a reference to a method that complied with our desired signature.
Java
Languages like Java don't have delegates. They use interfaces instead. The way they do that is to ask anyone who is interested in 'us being clicked', to implement a certain interface (with a certain method we can call), then give us the whole instance that implements the interface. We keep a list of all objects implementing this interface, and can call their 'certain method we can call' whenever we get clicked.