Question: What does prefix "On" implement in event cases in C# coding?


What does prefix "On" implement in event cases in C# coding?

Answers 1
Added at 2016-11-25 15:11

I think there is quite a confusion over the usage of "On" as prefix of a C# method.

In MSDN article "Handling and Raising Event" it says,

Typically, to raise an event, you add a method that is marked as protected and virtual (in C#) or Protected and Overridable (in Visual Basic). Name this method OnEventName; for example, OnDataReceived. The method should take one parameter that specifies an event data object. You provide this method to enable derived classes to override the logic for raising the event. A derived class should always call the OnEventName method of the base class to ensure that registered delegates receive the event.

indicating the On... method is to raise an event. However, in many coding samples, even some provided by Microsoft, we can see event the On method used as event handler, like on in here

First, let's populate the mouse and touch pointer event handlers. In the first event handler, OnPointerPressed(), we get the x-y coordinates of the pointer from the CoreWindow that manages our display when the user clicks the mouse or touches the screen in the look controller region.

void MoveLookController::OnPointerPressed(
_In_ CoreWindow^ sender,
_In_ PointerEventArgs^ args)
    // Get the current pointer position.
    uint32 pointerID = args->CurrentPoint->PointerId;
    DirectX::XMFLOAT2 position = DirectX::XMFLOAT2( args->CurrentPoint->Position.X, args->CurrentPoint->Position.Y );

    auto device = args->CurrentPoint->PointerDevice;
    auto deviceType = device->PointerDeviceType;
    if ( deviceType == PointerDeviceType::Mouse )
        // Action, Jump, or Fire

    // Check  if this pointer is in the move control.
    // Change the values  to percentages of the preferred screen resolution.
    // You can set the x value to <preferred resolution> * <percentage of width>
    // for example, ( position.x < (screenResolution.x * 0.15) ).

    if (( position.x < 300 && position.y > 380 ) && ( deviceType != PointerDeviceType::Mouse ))
        if ( !m_moveInUse ) // if no pointer is in this control yet
            // Process a DPad touch down event.
            m_moveFirstDown = position;                 // Save the location of the initial contact.
            m_movePointerPosition = position;
            m_movePointerID = pointerID;                // Store the id of the pointer using this control.
            m_moveInUse = TRUE;
    else // This pointer must be in the look control.
        if ( !m_lookInUse ) // If no pointer is in this control yet...
            m_lookLastPoint = position;                         // save the point for later move
            m_lookPointerID = args->CurrentPoint->PointerId;  // store the id of pointer using this control
            m_lookLastDelta.x = m_lookLastDelta.y = 0;          // these are for smoothing
            m_lookInUse = TRUE;

My questions being:

  1. Is there indeed such ambiguity over the usage of "On" prefix or is it just my misunderstanding? Do people really use "On" in both raising and handling event methods?
  2. What is the standard style of implementing the method raising and handling event? And what are the popular styles? What is the style that you suggest?
nr: #1 dodano: 2016-11-25 16:11

For the class that raises the event: when "some condition" occurs, it makes sense to call a method OnSomeCondition() in that class. Then if you want to inform an external party about about this condition, you'd have an event SomeCondition that you'd raise in the OnSomeCondition() method.

For the class that handles the event: when Visual Studio automatically generates a handler method, it calls it someClass_SomeCondition (at least in C#, which is what you've tagged your question). The second documentation exerpt and the example aren't C# though, which may explain the difference (I don't know if there's an "official" naming convention for event handlers).

However when you inherit from the class that raises the event, and the base class has followed the protected virtual recommendation, the word 'event' becomes ambiguous: you can still handle the SomeCondition event but you can also choose to override the OnSomeCondition() method.

So I wouldn't say that the On prefix "is used to raise an event", but "to handle a condition", and you can choose to raise an event in the OnCondition() method - and for the consuming side, you either handle an event or override the OnCondition() behavior. This is also what the first documentation exerpt says:

You provide this method to enable derived classes to override the logic for raising the event.

Source Show
◀ Wstecz