Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!