I thought that a base class would be an acceptable return signature but .Net Core 2.2 using visual studio code does not like it.
public class BasicRisk
{
public string Name {get;set;}
public string Amount{get;set;}
}
public class LowRisk : BasicRisk
{
public string Minimum { get; set; }
}
public class HighRisk : BasicRisk
{
public string Risk { get; set; }
}
Now at some level this is working because I can mock populate data like this.
public class MockRiskkData
{
private readonly List<HighRisk> _high = new List<HighRisk>();
public MockRiskkData()
{
_all.Add(
new HighRisk
{
Amount = "9384",
Name = "Johnny Dangerous",
Risk = "Extreme"
}
);
}
}
However when I use the Baseclass as return value in a method signature I get can't explicitly convert error. Now my understanding was that any inherited class that uses the base class will satisfy the return signature.
public class MockRiskkData
{
private readonly List<HighRisk> _high = new List<HighRisk>();
private readonly List<LowRisk> _low = new List<LowRisk>();
public MockRiskkData()
{
_all.Add(
new HighRisk
{
Amount = "9384",
Name = "Johnny Dangerous",
Risk = "Extreme"
}
);
}
public Task<List<BasicRisk>> GetRiskAsync(int risktype)
{
if(risktype == 1)
{
return Task.FromResult(_high); //Cannot implicitly convert type
}
return Task.FromResult(_low); //Cannot implicitly convert type
}
}
So clearly I am incorrect in my understanding. What can I try next?