When should you override OnEvent as opposed to subscribing to the event when inheritting

后端 未结 7 1943
梦谈多话
梦谈多话 2020-12-11 01:33

When should one do the following?

class Foo : Control
{
    protected override void OnClick(EventArgs e)
    {
        // new code here
    }
}
相关标签:
7条回答
  • 2020-12-11 02:39

    The event is for external subscribers. When you are deriving some control, always override the OnEvent method instead of subscribing to the event. This way, you can be sure when your code is called, because the actual event is fired when you call base.OnEvent(), and you can call this before your code, after your code, in the middle of your code or not at all. You can then also react on return values from the event (i.e. changed properties in the EventArgs object).

    0 讨论(0)
提交回复
热议问题