Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
Casting a list of derived class instances as a list of their base class instances

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?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda