I'd like to be able to have a method in a C# base class, callable on objects of several derived classes, that returns the object itself, and have the CLR know what type the object really is - i.e., the appropriate derived type. Can someone suggest a way to do it? Other, of course, than return type covariance, which C# doesn't have.
Something like this, except that Method()
's return type should be the type of the derived class, not the base:
public abstract class Base {
public Base Method() { return this; }
}
public class Derived1: Base { ... }
public class Derived2: Base { ... }
public class Main {
public static int Main() {
Derived1 d1 = new Derived1();
Derived1 x = d1.Method();
Derived2 d2 = new Derived2();
Derived2 y = d2.Method();
}
}
I can only think of two ways to make this work, and I don't like either of them:
Cast the result of Method() to the expected type (e.g., Derived1 x = (Derived) d1.Method();
). But casts are the tool of the Devil, and besides, the intent of the method is to return a Derived1
or Derived2
or ..., not a Base
.
Declare Method()
as abstract in the base and implement it separately in each derived class. But that runs exactly counter to the idea of factoring out common methods. The Method()
would be identical in each case except for its return type.
Okay, I misread the question. I'd thought the OP did want to override the method. Apparently not, so generics are the way forward:
public abstract class Base<T> where T : Base<T>
{
public T Method()
{
return (T) (object) this;
}
}
public class Derived1 : Base<Derived1>
{
}
There's still a cast, but that's unfortunately unavoidable as far as I'm aware. You'd almost certainly want to check it in the constructor, too:
public Base()
{
if (this as T == null)
{
// throw some exception
}
}
It's ugly, but it'll work... and the ugliness is confined to the base class.
Original answer
One way to do it would be to put Method
into a generic interface and implement it explicitly:
public interface IFoo<T> {
T Method();
}
public abstract class Base : IFoo<Base>
{
Base IFoo<Base>.Method()
{
return this;
}
}
public class Derived1 : IFoo<Derived1>
{
public Derived1 Method()
{
// If you need to call the base version, you'll
// need ((IFoo<Base>)this).Method()
return this;
}
}
It's not nice, but it would work... where possible, I think I'd try to avoid needing it, to be honest. (And yes, I've come across similar situations when implementing protocol buffers. It's annoying.)