Possible Duplicate:
Casting List<T> - covariance/contravariance problem
I have classes defined as below
public abstract class AbstractDType
{
protected abstract string Num1 { get; set; }
protected abstract string Num2 { get; set; }
}
public class AD1 : AbstractDType
{
public string Num1 { get; set; }
public string Num2 { get; set; }
public string Num3 { get; set; }
}
public class AD2 : AbstractDType
{
public string Num1 { get; set; }
public string Num2 { get; set; }
public string Num3 { get; set; }
}
public abstract class DTypeStrategy
{
protected virtual List<AbstractDType> GetData()
{
return new List<AD1>();
}
}
I would like to return a list of PD1 (concrete type) in GetData() method. However the above code threw a casting error . List<AbstractDType>
cannot be converted to List<PD1>
. How do I fix this error so that I could return concrete in GetData method.
There are other derived classes that inherited from DTypeStrategy which would be implementing GetData() such as below : ( I am assuming that I'll get the same casting error here as well )
public class MyDraw : DTypeStrategy
{
public override List<AbstractDType> GetData()
{
return new List <AD2>();
}
}