2

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 same 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.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
smriti
  • 445
  • 1
  • 4
  • 5
  • possible duplicate of [Help understanding .NET delegates, events, and eventhandlers ](http://stackoverflow.com/questions/2814065/help-understanding-net-delegates-events-and-eventhandlers) – H H Jul 24 '10 at 08:34
  • possible duplicate of [What are the differences between delegates and events?](http://stackoverflow.com/questions/29155/what-are-the-differences-between-delegates-and-events) – nawfal Jul 06 '14 at 20:44

7 Answers7

13

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 :)

sumit_batcoder
  • 3,369
  • 1
  • 25
  • 36
2

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

Community
  • 1
  • 1
Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
0

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).

Andrey Taptunov
  • 9,367
  • 5
  • 31
  • 44
  • 2
    *That's* not really true. Events *aren't* instances of delegates. They're pairs of add/subscribe methods really. And "delegate" is an ambiguous word - you can declare a delegate *type* and you can create a delegate *instance*. – Jon Skeet Jul 23 '10 at 20:14
  • *They're pairs of add/subscribe methods really.* But they are not *just* a pair of methods. The type of event's declaration *must be a* delegate type, isn't it? – Andrey Taptunov Jul 23 '10 at 20:24
  • I believe that delegates can be subscribed to events as handlers, but events are not delegates. – recursive Jul 23 '10 at 22:01
0

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'.

0

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.

Tiju John
  • 933
  • 11
  • 28
0

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;
Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • A delegate is a pointer to a method whose signature is fixed //except for the implied 'this' parameter//, along with a an object of suitable type to be passed to 'this' parameter. A key feature of delegates is that the caller of a delegate doesn't have to know anything about the type of its 'this' parameter; it's guaranteed to match whatever the method expects. – supercat Dec 07 '10 at 23:10
0

Here is a fantastic article by Jon Skeet on the subject, I recommend purchasing a copy of his book also.

Delegates and Events.

Matt
  • 2,730
  • 4
  • 28
  • 35