Information Technology Reference
In-Depth Information
Event Accessors
The last topic to cover in this chapter is event accessors. I mentioned earlier that the += and
-= operators were the only operations allowed for an event. These operators have the well-
defined behavior that you have seen so far in this chapter.
You can, however, change these operators' behavior and have the event perform whatever
custom code you like when they ar
e used. You can do this by defining event accessors for
the event.
There are two accessors: add and remove .
￿
￿
The declaration of an event with accessors looks similar to the declaration of a property.
The following example shows the form of an event declaration with accessors. Both acces-
sors have an implicit value parameter called value , which takes a reference to either an
instance method or a static method.
public event EventHandler Elapsed
{
add
{
... // Code to implement the =+ operator
}
remove
{
... // Code to implement the -= operator
}
}
When event accessors are declared, the event does not contain an embedded delegate
object. You must implement your own storage mechanism for storing and removing the meth-
ods registered with the event.
The event accessors act as void methods, meaning that they cannot use return statements
that return a value.
Search WWH ::




Custom Search