When should you call base.Method() in overridden method, and how to mark this when you write code in team?

前端 未结 3 1087
轻奢々
轻奢々 2020-12-15 04:32

When using some framework/api, sometimes it\'s pretty unclear if you must call base.Method if you override it, for example you can be pretty sure that you should call base.M

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 05:16

    If you are requiring that consumers of your class MUST implement functionality of a particular method, that method should be marked abstract.

    If consumers of your class should optionally provide functionality of a particular method, that method should be virtual.

    There is really no way to require that a consumer of a class call a base.Method() on a virtual method. It really depends on context. If the base.Method() does some work that you'd otherwise have to do, it'd behoove you to call base.Method() if that would save you some development time/it makes sense in that context.

提交回复
热议问题