events and delegates dilemma
I am really in a fix over understanding the concept of events and delegates.I know that delegates are the objects holding references to methods and can call methods having the sam开发者_JAVA技巧e return type and parameters, but then what exactly are events?
If I need to use events for making a simple calculator, then how can I use them so that there are 3 events: one that can be used one for digit, one for the operators and the other for the equal or the result operation.
Delegate is basically a Method Pointer. A delegate lets us create a Reference Variable, but instead of referring to an instance of a Class, it refers to a Method inside the class. It refers any method that has a return type and has same parameters as specified by that delegate. It's a very very useful aspect of Event.
Event is simply something that's happened in our program. Once, it happens, the objects which subscribed to that event respond to that event. In real world example its simple to understand. I would take an example of Cricket. Let's take Hitting the Ball as an event. So, once the ball is hit by a batsman, Fielder runs towards it, Umpire tracks the ball, as well as batsman keeps an eye to where the ball is. The audience is ready to jump n shout. So, here hitting the Ball is an event. Batsman, Umpire, Fielder and the Audience are the objects who respond to that event. Event is totally unaware of who's going to respond to it and in what way. The objects who respond need to Subscribe to that event first and after that whenever the event is fired, they are notified and can Handle that event in whatever way they want like our Fielder, Umpire, Audience, Batsman do it in their own ways. In dotnet, they handle events by using eventhandlers. This is just a brief description relating to the real world scenario. For further reading and have a good understanding please read the topics in Head First C# by O'Reilly. It really explains these topics beautifully relating to real world examples. Once you go through it, you'll be able to grab it and keep it in mind. Hope it helps :)
Lotta good questions with good answers here around events and delegates. Give them a look:
Difference between events and delegates and its respective applications
What are the differences between delegates and events?
Where do I use delegates?
finally, never underestimate the value of a Jon Skeet article:
http://pobox.com/~skeet/csharp/events.html
I know dat delegates are the objects
This is not realy true. Delegates - are types. Events - are instances of delegates (that are marked with special keywords to generate some additional staff by complier).
You can see events as a collection of delegate instanses. A subscriber/listener to an event registrars it self by supplying a delegate instans.
At one point the 'owner' of that event can raise it. Which will call all the delegate instanses in the 'collection'.
delegates are indeed pointers which point to a method of its own signature. You can see an event as a pointer to the list of delegates. In the way of invocation, they both are much the same.
The difference can be understood if you see the Observer design pattern, there are multiple subscribers to a single event. The Publisher class will detect some 'Event' and raise it and all the subscribers will get called.
like you create a button class and the container wants to subscribe to the click event, it will attach its handler to the click event which your button class raises..
In your calculator form, the input can be buttons, in the code behind you can make a single method and by the event args, make out which button is clicked and apply the logic.
A delegate is essentially a collection of one or more references to methods with identical method signatures. In c#, +
(or +=
) is used to add new methods to the delegate and -
(or -=
) is used to remove methods from the delegate.
An event is something that can be raised in the code to then call all the methods connected to its delegate. Events almost always have delegates that return void
with two arguments: Object sender
and the event arguments, which are always a class derived from System.EventArgs
.
For example, if I wanted to write an event OnCookFood in my Chef class. Note: This assumes I wrote a CookEventArgs
class first, because I'd presumably want to pass what kind of food my Chef is cooking.
// modifier delegate void HandlerName(Object sender, EventArgsClass e)
// modifier event HandlerName EventName
public delegate void CookFoodHandler(Object sender, CookEventArgs e);
public event CookFoodHandler OnCookFood;
// More code...
OnCookFood(new CookEventArgs("Pie"));
Of course, this is the hard way to do it. You can use the EventHandler<T>
class to have the compiler create the delegate for you:
public event EventHandler<CookEventArgs> OnCookFood;
// More code...
OnCookFood(new CookEventArgs("Pie"));
and finally, to add a handler; assuming we have an object cook
:
void HandleCooking(Object e, CookEventArgs e) {
// Do something here
}
// in another function, probably the constructor...
cook.OnCookFood += HandleCooking;
Here is a fantastic article by Jon Skeet on the subject, I recommend purchasing a copy of his book also.
Delegates and Events.
精彩评论