1

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 not 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 feeling 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?

  • 4
    Questions about naming conventions aren't opinion-based. There is a naming convention; that statement is a fact. I want to know what it is. – rory.ap Nov 13 '18 at 15:14
6

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.