For instance, I have two classes performing the same function:
class HDD : Disk
{
public HDD RMA()
{
HDD newHDD = RMAService.Exchange(this);
return newHDD;
}
}
class SSD : Disk
{
public SSD RMA()
{
SSD newSSD = RMAService.Exchange(this);
return newSSD;
}
}
What I would like is to implement the RMA function in the base class, but maintain the strongly typed return value, so that the user can be certain by the function signature that they are getting back the type of disk they sent in!
What I have tried so far:
(see solutions below)
This type definition is ugly though. Anyone creating their own Disk class or a reference to a Disk would have a hard time knowing how to correctly use the type.
There's also no way to constrain the type argument to be exactly the class being defined. It just seems odd that there isn't a specialized way of declaring a property or method in a base class where the compile time type is whatever the derived type is.