Delegates and Events in C# - Event Accessors
(Page 4 of 4 )
An event’s accessors are the implementations of its += and -= functions. By default, accessors are implemented implicitly by the compiler. Consider this event declaration:
public event EventHandler PriceChanged;
The compiler converts this to the following:
- A private delegate field
- A public pair of event accessor functions, whose implementations forward the+=and-=operations to the private delegate field
You can take over this process by defining explicit event accessors. Here’s a manual implementation of thePriceChangedevent from our previous example:
private EventHandler _PriceChanged; // declare a private delegate
public event EventHandler PriceChanged
{
add
{
_PriceChanged += value;
}
remove
{
_PriceChanged -= value;
}
}
This example is functionally identical to C#'s default accessor implementation. The add and remove keywords after the event declaration instruct C# not to generate a default field and accessor logic.
With explicit event accessors, you can apply more complex strategies to the storage and access of the underlying delegate. There are three scenarios where this is useful:
- When the event accessors are merely relays for another class that is broadcasting the event.
- When the class exposes a large number of events, where most of the time very few subscribers exist, such as a Windows control. In such cases, it is better to store the subscriber’s delegate instances in a dictionary, since a dictionary will contain less storage overhead than dozens of null delegate field references.
- When explicitly implementing an interface that declares an event.
Here is an example that illustrates the last point:
public interface IFoo
{
event EventHandler Ev;
}
class Foo : IFoo
{
private EventHandler ev;
event EventHandler IFoo.Ev
{
add { ev += value; }
remove { ev -= value; }
}
}
Theaddandremove parts of an event are compiled toadd_XXX andremove_XXX methods.
The+=and-=operations on an event are compiled to calls to theadd_XXX andremove_XXX methods.
Event Modifiers
Like methods, events can be virtual, overridden, abstract, and sealed. Events can also be static:
public class Foo
{
public static event EventHandler<EventArgs>StaticEvent;
public virtual event EventHandler<EventArgs>VirtualEvent;
}
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Check it out today at your favorite bookstore. Buy this book now.
|
|