0
public delegate void EventHandler(object sender, EventArgs e);
public class Button: Control
{
   public event EventHandler Click;
   protected void OnClick(EventArgs e) {
      if (Click != null) Click(this, e);
   }

}

The book explains the code above in the following way:

The OnClick method in the Button class "raises" the Click event. The notion of raising an event is precisely equivalent to invoking the delegate represented by the event — thus, there are no special language constructs for raising events. Note that the delegate invocation is preceded by a check that ensures the delegate is non-null.

Why does it imply that there's only one delegate in the event when there's a delegate instance for every event-handler? An instance is an instance and an event can contain many delegate instances that connect to actual methods.

I'd greatly appreciate if someone can make sense of this paragraph for me.

Garrett Biermann
  • 537
  • 1
  • 4
  • 12

2 Answers2

0

This is in depth and might not be exactly what you are looking for. But, it breaks down the differences between events and delegates in pretty good detail...taking it all the way to the IL.

Otherwise: this SO has already been answered on a simpler basis

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
0

Now what does it mean by the "delegate represented by the event"?

This is how the event looks like from the perspective of the code that raises it. The event is raised using the standard method call syntax (as opposed to some "special" syntax), which in turn calls all the subscribed event handlers in no particular order.

And what does it mean by "a check that ensures the delegate is non-null"?

If there are no event handlers subscribed to the event, then the "delegate representing the event" is null. So to raise the event, you should do something like this:

class MyClass {

    public delegate void MyEventHandler(MyEventArg arg);

    public event MyEventHandler MyEvent;

    void RaiseMyEvent(MyEventArg arg) {
        var handler = MyEvent;
        if (handler != null) // Any subscribers?
            handler(arg);
    }

}

BTW, copying the event to a local variable (handler) is considered a good practice, for this reason.

Community
  • 1
  • 1
Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
  • Why does it imply that there's only one delegate in the event? I mean, the event is a function member that's declared with a delegate type. Meaning, instances of that delegate type referencing methods of that delegate type and signature are added to the event as event handlers. – Garrett Biermann Apr 10 '13 at 03:08
  • @user2247389 _"Why does it imply that there's only one delegate in the event?"_ Because there is. That delegate then calls all the other delegates (i.e. event handlers subscribed to the event). – Branko Dimitrijevic Apr 10 '13 at 03:12
  • But a delegate can reference only one method, unless it's a composite delegate. We have each instance of the delegate type associated with one event handler. Where does this single delegate that calls the other delegates come from? Is it a composite delegate instance that's made of all the instance delegates in the event? – Garrett Biermann Apr 10 '13 at 03:21
  • @user2247389 This is a "multi-cast" delegate. Where it "comes from" is an implementation detail. – Branko Dimitrijevic Apr 10 '13 at 10:04