Information Technology Reference
In-Depth Information
An Event Has a Private Delegate
There's good reason for the similarities in the behaviors of delegates and events. An event con-
tains a private delegate, as illustrated in Figure 16-2. The important things to know about an
event's private delegate are the following:
An
event
gives structured access to its privately controlled delegate.
Unlike the many operations available with a delegate, with an event, you can only add,
remove, and invoke event handlers.
When an event is raised, it invokes the delegate, which sequentially calls the methods in
the invocation list.
Notice in Figure 16-2 that only the
+=
and
-=
operators are sticking out to the left of the
event. This is because they are the only operations allowed on an event.
Figure 16-2.
An event as an encapsulated delegate
Figure 16-3 illustrates the runtime view of a publisher class with an event called
Elapsed.
ClassA
and
ClassB
, on the right, each have an event handler registered with
Elapsed
. Inside the
event you can see the delegate referencing the two event handlers. Besides the event, the pub-
lisher also contains the code that raises the event.
Figure 16-3.
Structure and terminology of a class with a timer event