Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8914007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:37:07+00:00 2026-06-15T04:37:07+00:00

Given the following class structure: class Base {} class DerivedA: Base {} class DerivedB:

  • 0

Given the following class structure:

class Base {}
class DerivedA: Base {}
class DerivedB: Base {}
class Container
{
    public List<Base> Items { get; }
}

where the list of Derived objects grows over time as my application develops more functionality. I’m trying to do:

Container container = ReturnsContainer();
foreach (var item in container.Items)
{
    doStuff(item);
}

where doStuff(item) is overloaded by derived type.

void doStuff(DerivedA) {}
void doStuff(DerivedB) {}

That doesn’t work as the compiler says “cannot convert from ‘Base’ to ‘DerivedA'” and “The best overloaded method match doStuff(DerivedA) has some invalid arguments”. The best approach I can come up with is:

Container container = ReturnsContainer();
foreach (var item in container.Items)
{
    if (item is DerivedA)
    {
        doStuff((DerivedA)item);
    }
    else if (item is DerivedB)
    {
        doStuff((DerivedB)item);
    }
}

Any thoughts on how I can make this cleaner? Since my Derived types will only grow over time, I would have to go back and add to this long if structure to continue to make this work.

I think the container.Items.OfType<> solution would translate to a growing (and unnecessary) performance hit as the number of Derived types increases.

Do I have to use generic delegates for this? Seems like an overly complex solution for something that feels like it should be simple polymorphism.

Edit: For further clarity, let’s say the Base and Derived type hierarchy is on a separate API layer and is not modifiable (final classes). The solution has to work with the existing inheritance structure and cannot extend the Base and Derived objects. Though the API layer can grow new Derived classes as time goes on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:37:09+00:00Added an answer on June 15, 2026 at 4:37 am

    There are a couple of options here, but it really boils down to how can doStuuf know what to do if you’re going to add new derived types in the future?

    So, one option is if Container really does only contain 1 type at a time then you could make it generic:

    class Container<T> where T: Base
    {
        public List<T> Items { get; }
    }
    

    Now when you iterate it, you know that Items is a List of DerivedA or DerivedB

    Container<DerivedA> container = ReturnsContainerOfDerivedA();
    foreach (var item in container.Items)
    {
        doStuff(item); // item is definately DerivedA
    }
    

    This would mean you either have multiple doStuff methods

    public void doStuff(DerivedA item){...}
    public void doStuff(DerivedB item){...}
    

    or if more appropriate you can have 1 which takes the base

    public void doStuff(Base item){...}
    

    That is the advantage of simple polymorhism!


    The second option is to define doStuff as taking a dynamic if you’re using C#3.5

    public void doSomething(dynamic item){..}
    

    But I would personally avoid this option!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following structure... class Foo { public string Category { get; set; }
Given the following class: class A { public List<B> ListB; // etc... } where
Given the following object: public class Product { string Name {get;} int Quantity {get;}
Given the following class structure: class Base { virtual void outputMessage() { cout <<
Given the following class structure, will Bar serialize/deserialize as expected? public class Foo {
Given the following structure: Public Class Vendor Public Property Accounts As Account() End Class
Given the following code: public class Product { public Guid Id {get;set;} } public
Given the following classes: public class Lookup { public string Code { get; set;
Given the following multiton: public class Multiton { private static final Multiton[] instances =
Given the following types private class MyTestDummyClassValidationDef : ValidationDef<MyTestDummyClass> { ... } public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.