Overriding Extension Methods

前端 未结 9 1296
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 13:57

I\'ve been thinking about using extension methods as a replacement for an abstract base class. The extension methods can provide default functionality, and can be \'overridd

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 14:02

    I agree with Michael. Base classes should contain all base functionality Extension methods should, obviously, extend the base functionality. In dynamic languages like Ruby it is often typical to use extension methods to provide addition functionality instead of using subclasses. Basically, extension methods are there to replacing using subclassses, not to replace using base classes.

    The only exception to this that I've seen is if you have multiple type that have different class hierachies (like winform controls), you can make a subclass of each that all implement and interface and then extend that interface, thereby giving "base" functionality to a group of different controls, without extending everything like Control or Object.

    Edit: answering your second question

    I think the compiler will catch this for you.

提交回复
热议问题