I believe there must be some difference between them, such as,
which has higher performance?
how to implement these OnXXX() by MS?
how to determine which one is preferred in a specific case?
The OnXXX is simply a method that raises an event and optionally perfoms
additional processing before or after raising the event. So, for example,
if you've got an event called Validated in your class, if you want to
implement your class in the Microsoft way, the OnValidated method would
look like that:
protected void OnValidated()
{
if (Validated != null)
{
Validated(/* event params here */
}
}
Then, whenever you need to raise the Validated event from somewhere in your
class, simply call OnValidated().
The OnXXX serves several purposes:
1) making it easier for methods in your class to raise an event: instead of
testing if somebody has registered to the event and then raise it, simply
call OnXXX
2) Performs some pre- or post-event processing. E.g., if you've got a
m_validated boolean that you want to set to true whenever something has
been validated, instead of setting it in every method that raises the
Validated event, do it in the OnValidated method:
protected void OnValidated()
{
m_validated = true;
if (Validated != null)
{
Validated(/* event params here */
}
}
3) Allow derived classes to control the event flow and pre- or post-event
processing. For example, if one of the derived class doesn't want the
Validated event to be raised, it can override the OnValidated() method and
do nothing (not such a good idea as the OnValidated method might do more
than just raise the event). Generally though, this is used to perform
additional pre- or post-processing, often used in Control derived classes
to do some custom painting in the OnPaint method. I'm not being very clear
here so have a look at Bob Powell's explanation here which is far better
that mine:
<
http://www.bobpowell.net/overrideorhandle.htm>