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

154
Visualizações
How to handle derived classes featuring different parameters

I am new to C# and I have a project in which I have a parent class Task with derived classes FixedTask and RelativeTask.

FixedTask and RelativeTask have different properties, named differently. And I need to perform some computations with each Task to compute some workload.

A Project contains different Tasks which can be of the various types e.g. FixedTask and RelativeTask. I was planning on doing something like this:

foreach (Task Task in this.Tasks)
{
    switch (Task)
    {
        case FixedTask:
            switch (Task.Frequency)
            {
                case "Daily":
                    // do some calculations
                 
                // other frequency cases
            }
        // other Task cases
    }

But I receive an error that Task does not have a particular parameter because it is implemented in FixedTask but not in RelativeTask.

I would have though that the switch pattern allowed C# to understand which type he has to consider but apparently not.

What is the proper way of achieving what I want?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Pattern matching: case FixedTask f.

But you shouldn't. Your base class should not know nor care how the derived classes are implemented.

The purpose of OO and inheritance is that derived classes contain their own logic. So if your task runner wants to execute a task, do so:

foreach (Task task in this.Tasks)
{
    task.Execute();
}

And then each Task-deriving class has the logic in their overridden Execute() method.

over 4 years ago · Santiago Trujillo Relatório

0

Pattern matching helps:

foreach (Task Task in this.Tasks)
        {
            switch (Task)
            {
                case FixedTask ft:
                    switch (ft.Frequency)
                    {
                        case "Daily":
                        ...
                    }
                case RelativeTask rt:
                    switch (rt.OtherProperty)
                    {
                        case ...:
                    }
over 4 years ago · Santiago Trujillo Relatório

0

As a quick (but not the best) amendment, I suggest pattern matching, i.e.

foreach (Task task in Tasks) {
  if (task is FixedTask fixedTask) {
    // From now on you work with fixedTask
    switch (fixedTask.Frequency) {
      ...
    }
  } 
  else if (task is RelativeTask relativeTask) {
    // From now on you work with relativeTask
  } 
}
over 4 years ago · Santiago Trujillo Relatório
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