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 993957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:30:20+00:00 2026-05-16T06:30:20+00:00

In C# I have a bunch of objects all inheriting from the same base

  • 0

In C# I have a bunch of objects all inheriting from the same base class.
I also have a number of Dictionaries, one for each subclass.
What I want to do is add all of those Dictionaries to one List so I can loop through them all and do some work (like comparing the lists etc).

In summary

Dictionary<string, Child> childObjects = new Dictionary<string, Child>();
List<Dictionary<string, Parent>> listOfDictionaries = new List<Dictionary<string, Parent>>();
listOfDictionaries.Add(childObjects);

I would have thought that since Child inherits from Parent, this should work, but it won’t compile. Clearly I am not understanding something about inheritance and generics 🙂

A full code example

class Program
{
    static void Main(string[] args)
    {
        //Creating a Dictionary with a child object in it
        Dictionary<string, Child> childObjects = new Dictionary<string, Child>();
        var child = new Child();
        childObjects.Add(child.id, child);

        //Creating a "parent" Dictionary with a parent and a child object in it
        Dictionary<string, Parent> parentObjects = new Dictionary<string, Parent>();
        parentObjects.Add(child.id, child);
        var parent = new Parent();
        parentObjects.Add(parent.id, parent);

        //Adding both dictionaries to a general list
        List<Dictionary<string, Parent>> listOfDictionaries = new List<Dictionary<string, Parent>>();

        listOfDictionaries.Add(childObjects);  //This line won't compile

        listOfDictionaries.Add(parentObjects);


    }
}

class Parent
{
    public string id { get; set; }
    public Parent()
    {
        this.id = "1";
    }
}

class Child : Parent
{
    public Child()
    {
        this.id = "2";
    }

}

Is there any way of achieving this?

  • 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-05-16T06:30:21+00:00Added an answer on May 16, 2026 at 6:30 am

    You can’t do this safely. Imagine you did this:

    listOfDictionaries[0]["foo"] = new Parent();
    

    That looks fine – but it would mean that childObjects would contain a value which isn’t an instance of Child!

    C# 4 has introduced restricted generic variance where it’s safe – so you can convert a reference of type IEnumerable<Banana> to IEnumerable<Fruit> for example – but what you’re wanting to do here isn’t safe, so it still isn’t allowed.

    If you could tell us more about the bigger context – what you’re trying to achieve – we may be able to help more. Can you give examples of what you’d want to do with the list afterwards?

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

Sidebar

Related Questions

I have a bunch of Objects from the same Class in Python. I've decided
I have a bunch of COM objects which all implement the same interface, and
I'm working on a project where I have a bunch of objects (same type)
I have a bunch of different objects that are commonly edited in the same
I currently have a bunch of SQLCHAR objects from a database query. The query
I have a bunch of Widget objects. Now each widget has a string property
I have a bunch of objects that inherit abstracts interfaces generated from an idl
I have a bunch of simple data objects of different types (all properties are
Have one object of type A that is related to a bunch of objects
I have a table that stores a bunch of objects. Each object can have

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.