Event Naming Guidelines
The following rules outline the naming guidelines for events:
- Use Pascal case.
- Do not use Hungarian notation.
- Use an
EventHandler
suffix on event handler names. - Specify two parameters named sender and e. The sender parameter represents the object that raised the event. The sender parameter is always of type object, even if it is possible to use a more specific type. The state associated with the event is encapsulated in an instance of an event class named e. Use an appropriate and specific event class for the e parameter type.
- Name an event argument class with the
EventArgs
suffix. - Consider naming events with a verb. For example, correctly named event names include Clicked, Painting, and DroppedDown.
- Use a gerund (the "ing" form of a verb) to create an event name that expresses the concept of pre-event, and a past-tense verb to represent post-event. For example, a Close event that can be canceled should have a
Closing
event and aClosed
event. Do not use theBeforeXxx
/AfterXxx
naming pattern. - Do not use a prefix or suffix on the event declaration on the type. For example, use
Close
instead ofOnClose
. - In general, you should provide a protected method called OnXxx on types with events that can be overridden in a derived class. This method should only have the event parameter e, because the sender is always the instance of the type.
The following example illustrates an event handler with an appropriate name and parameters.
Public Delegate Sub MouseEventHandler(sender As Object, e As MouseEventArgs)
[C#]
public delegate void MouseEventHandler(object sender, MouseEventArgs e);
The following example illustrates a correctly named event argument class.
Public Class MouseEventArgs
Inherits EventArgs
Dim x As Integer
Dim y As Integer
Public Sub New MouseEventArgs(x As Integer, y As Integer)
me.x = x
me.y = y
End Sub
Public Property X As Integer
Get
Return x
End Get
End Property
Public Property Y As Integer
Get
Return y
End Get
End Property
End Class
[C#]
public class MouseEventArgs : EventArgs
{
int x;
int y;
public MouseEventArgs(int x, int y)
{ this.x = x; this.y = y; }
public int X { get { return x; } }
public int Y { get { return y; } }
}
See Also
Design Guidelines for Class Library Developers | Event Usage Guidelines