Question

From what I've observed, methods that start with the word "On" fall into two categories:

  1. A base class that defines an event will expose a protected method whose name starts with "On" followed by the event name in order for derived classes to be able to raise the event.

  2. A base class exposes a protected virtual or protected internal virtual method whose name starts with "On" which can be overridden by derived classes that want to replace the default behavior of that method.

#1 is straightforward and reasonable to me. By allowing the derived class to override the method, it guarantees that the event will be raised if and when the derived class calls the method.

#2 leaves me wondering, though, because I'm not sure if the associated or implied event in the base class will ever be raised unless the overridden method calls base.On.... In the protected internal virtual case, the method can even be called from other classes.

One example of #2 is Microsoft.EntityFrameworkCore.DbContext: when the code generation automatically produces your derived class, it has two methods: protected override void OnConfiguring and protected override void OnModelCreating. Neither of the methods call the base class in the generated bodies. In this case -- since they are protected not protected internal, the base class must call these methods at some point. I'm not sure if there is even an event involved.

Another example of #2 is System.Windows.Forms.Form. There are many protected virtual methods, for instance protected virtual void OnActivated. The description of that method is "Raises the System.Windows.Forms.Form.Activated event.", but of course that only happens if the method calls base.OnActivated.

Is there a definitive convention for using the word "On" at the beginning of certain method names? Must there always be an event involved, and should that event name always be used after the "On" in the method name? If an event is always associated with a method On..., what can clients of your API assume will happen if they don't call base.On... when they override the method? Can they assume the event won't be fired? If the latter is true, then why does the auto-generated code for Microsoft.EntityFrameworkCore.DbContext.OnConfiguring and Microsoft.EntityFrameworkCore.DbContext.OnModelCreating not call the base class?

Was it helpful?

Solution

The convention is that On____ methods are ones triggered by some other occurrence (described by the blank). Sometimes that is a literal C# event, sometimes not. How it is triggered is an implementation detail. The name is meant to convey the design of “this triggers after _______”, not an implementation detail.

OTHER TIPS

Prefix "on" is mainly used when you want it to be used as a callback. It means these will not be called directly, but will be called after some event fired.

For example, when you write method called onClick then it will not call directly but it requires some user interaction and when user clicks on it in that case only it will fire the particular method or action. For example following are some methods rather say events in the javascript are as follows:

onchange An HTML element has been changed onclick The user clicks an HTML element onmouseover The user moves the mouse over an HTML element onmouseout The user moves the mouse away from an HTML element onkeydown The user pushes a keyboard key onload The browser has finished loading the page

Licensed under: CC-BY-SA with attribution
scroll top