Information Technology Reference
In-Depth Information
An Event Is a Member
A common error is to think of an event as a type, which it is not. An event is a member , and there
are several important ramifications to this:
￿
Because an event is a member
-
It must be declared in a class or struct, with the other members.
-
You cannot declare an event in a block of executable code.
Because a member is not a type, you do not use a new expression to create its object. That
is done automatically when its class or struct object is created.
￿
The Delegate Type and EventHandler
An event declaration requires the name of a delegate type . You can either declare one or use one
that already exists. If you declare a delegate type, it must specify the signature and return type
of the methods that will be stored by the event.
A better idea is to use the predefined delegate type used by the .NET BCL and designated
as the standard for use with events. You are strongly encouraged to use it. It is the EventHandler
delegate, and its declaration is shown in the following code. Delegate EventHandler is covered
in more detail later in this chapter.
public delegate void EventHandler(object sender, EventArgs e);
Search WWH ::




Custom Search