Well, I have several different classes based on a base class. This base class is abstract, containing commom methods.
One method is the Copy method, which should be present in all derived classes, so I put it in the base class. BUT, I want it to return the derived type not the base and not the object.
The solution I got for this uses a parameter like:
abstract class CopyableClass<T> { public abstract T Copy(); } class DerivedClass : CopyableClass<DerivedClass> { public override DerivedClass Copy() {
So the main goal here is
Remove the type parameter in the base class and still force the method to return the corresponding derived type.
One workaround.
The best I could do so far is one of the comments below, but it still uses a common parameter
abstract class BaseClass { //base methods not related to deriving type } interface ICopyable<T> { T Copy(); } class DerivedClass : BaseClass, ICopyable<DerivedClass> { public DerivedClass Copy() { //do what is needed for copy and return a new DerivedClass } }
source share