Return type of child class from parent class method

What you are asking is not strictly polymorphism, since you want to override a return type, and thus have a different signature from the original method. True polymorphism changes none of this signature, so what you're asking isn't a first-class situation in C#.

That being said, there are at least two ways about it.

The simplest way is to override the method, using the new keyword to hide the original method, allowing you to change the signature.

public new B MyMethod() { return (B)(base.MyMethod()); }

This will allow any usages of B to return a B, but does not actually override the behaviour of A.MyMethod. It can be useful to avoid repetitive typecasting in code.

if you need to also override the method to allow it to return an object of type B, this cannot coexist in the same class, because the compiler sees the same signature (same name, same parameters, even though the return type is explcitly different).

The second way is called Static Polymorphism, and is very useful if you're using a factory of some kind, but can bevery tricky to work around, because it allows exactly one level of actual inheritance. A Statically Polymorphic type starts with a type that has a Generic component constrained against itself:

public class A<TSelf> where TSelf : A<TSelf> { }
public class B : A<B> { }

This means your method could return an object of type TSelf or even make this an abstract method so each inheritor has to deal with the constructor, but any class inherited from B has a harder time overriding this, since it's inheriting from A<B>, not A<C>

Tags:

C#

Inheritance