Search code examples
c#inheritanceextension-methodsabstraction

Extending class hierarchy


I have an abstract base class:

public abstract class BaseClass
{
    public double CommonMethodForAllSubClasses(double parameter)
    {
        //common implementation
        return 0;
    }

    public abstract double MethodThatMustBeDefinedInSubClasses(double parameter);
}

And a class hierarchy under it.

I want to make, in another assembly, an Extension class for Base class and its subclasses. I thought to do it with a static class with a methods which receive as parameter:

public static Method(this BaseClass, ...)

But I want this static Method to be defined for all subclasses from BaseClass. I know I can't do a static abstract method but I want to avoid to implement these methods in the class itself... And it would be very good that the compiler "tells me" that these methods must be redefined for sub-classes.


Solution

  • But I want this static Method to be defined for all subclasses from BaseClass.

    Your extension method would be callable from instance of derived class. But I guess that's not what you meant.

    And it would be very good that the compiler "tells me" that these methods must be redefined for sub-classes.

    Implementing it as a regular abstract method on the base class is the only way to achieve that.