I have a subclass with an over-ridden method that I know always returns a particular subtype of the return type declared in the base class. If I write the code this way, it
Change your method signature on Derived class to:
public override BaseReturnType PolymorphicMethod()
{
return new DerivedReturnType();
}
C# doesn't support variant return types. You can check out this post for a way to do this using Generics...http://srtsolutions.com/blogs/billwagner/archive/2005/06/17/covaraint-return-types-in-c.aspx
Here's a sample using Generics in your model:
public class BaseReturnType
{
}
public class DerivedReturnType : BaseReturnType
{
}
public abstract class BaseClass where T : BaseReturnType
{
public abstract T PolymorphicMethod();
}
public class DerviedClass : BaseClass
{
public override DerivedReturnType PolymorphicMethod()
{
throw new NotImplementedException();
}
}