I'm trying to return a list derived classes (RedThingSets and BlueThingSets) as a list of their base (ThingSets) class so that I can have a more generic function returning them (this is just an example to illustrate).
(This is not actual code)
public abstract class ThingSet
{
public List<Thing> Things;
}
public class RedThingSet : ThingSet {}
public class BlueThingSet : ThingSet {}
public void MahClass()
{
public List<RedThingSet> RedThingsList = new List<RedThingSet>();
public List<BlueThingSet> BlueThingsList = new List<BlueThingSet>();
public List<ThingSet> GetThingSet( bool giveMeTheBlue )
{
if ( giveMeTheBlue )
{
return BlueThingsList;
}
else
{
return RedThingsList;
}
}
}
Is this possible?
I've tried using as
like this: return BlueThingsList as List<ThingSet>;
And I've tried casting it directly like this: return (List<ThingSet>) BlueThingsList;
But neither are working,
Am I just approaching this entirely wrong?