2

I have a base class (A) that I have built with some base functionality, and now want to create a subclass (B) with some custom functionality from an external third class (C), which can still be treated as an object of type A.

As I understand it, C# doesn't allow a class to inherit from more than one class, and the typical approach to this kind of problem is to change the base class to an interface, so that the subclass can still inherit from C, and implement A as an interface. However I also want A to provide some default method implementations, which B may override and call. I can't seem to find any way to call A.someMethod() from B if B has it's own implementation of someMethod(). I've put together a code example which hopefully shows what I'm trying to achieve:

  1. Base Case:
public class A {
    // Contains default method implementations and properties
    
    public virtual int someMethod(int i){
        return i;
    }
}


public class B: A{
    // B does not inherit C
    public override int someMethod(int i){
        return i*base.someMethod(i);
    }
}

public class C{
    // Package class which B must subclass from
}

  1. B Cannot Inherit from A and C:
public class A {
    // Contains default method implementations and properties
    
    public virtual int someMethod(int i){
        return i;
    }
}

public class B: C, A {
    // Theoretically solves the problem, but C# doesn't support multiple inheritance with classes
    public override int someMethod(int i){
        return i*base.someMethod(i);
    }
}


public class C{
    // Package class which B must subclass from
}
  1. Can't access default implementation in B, calling someMethod results in a stack overflow
public interface A {
    // Contains default method implementations and properties
    
    public virtual int someMethod(int i){
        return i;
    }
}


public class B: C, A {
    // Making A an interface allows B to be stored in a list of type A, but how can I access the default implementation?
    int A.someMethod(int i){
        return i*(this as A).someMethod(i);
    }
}

public class C{
    // Package class which B must subclass from
}
  1. Only working solution that I have, but seems very clunky:
public interface A {
    // Contains default method implementations and properties
    
    public virtual int someMethod(int i){
        return _someMethod(i);
    }
    
    public virtual int _someMethod(int i){
        return i;
    }
}


public class B: C, A {
    int A.someMethod(int i){
        return i*(this as A)._someMethod(i);
    }
}

public class C{
    // Package class which B must subclass from
}

Is option 4 the best approach? Or is there a better way to do this?

  • 1
    Does this answer your question? [C# 8 base interface's default method invocation workaround](https://stackoverflow.com/questions/59398027/c-sharp-8-base-interfaces-default-method-invocation-workaround) – Marwie Nov 07 '22 at 16:25
  • Thanks for the response @Marwie! I think that does answer my question, as it looks like this is something that may be supported in C# someday, but isn't yet, so I guess I'll still with option 4 for now. – sapphirewhale Apr 02 '23 at 03:18

0 Answers0