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

  • Home
  • SEARCH
  • 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 8528163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:44:30+00:00 2026-06-11T08:44:30+00:00

I have base class that is called Entity. Then I have two child class

  • 0

I have base class that is called Entity.
Then I have two child class Tag, and Property that inherits from Entity.
Now I want to have dictionary that stores a List of Entity. But I cannot get it to work. Have a done the inheritance wrongly?

Dictionary<string, List<Entity>> baseDict = new Dictionary<string, List<Entity>>();

List<Tag> tags = new List<Tag>();
tags.Add(new Tag("2012"));
tags.Add(new Tag("hello"));
tags.Add(new Tag("lego"));
List<Properties> properties = new List<Properties>();
properties.Add(new Properties("Year"));
properties.Add(new Properties("Phrase"));
properties.Add(new Properties("Type"));

baseDict.Add("Tags", tags);
baseDict.Add("Properties", properties);
  • 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-11T08:44:31+00:00Added an answer on June 11, 2026 at 8:44 am

    This is a common mistake.

    A List<Derived> does not automatically inherit from a List<Base>, and they cannot be used interchangeably. The reason for this is that a list is a mutable structure, i.e. elements can be added, removed, and modified.
    For example, if I have a List<Dog> and a List<Cat> lists, and I was able to treat them as a List<Mammal>, then the following code will be possible:

    List<Dog> dogs = new List<Dog>();  //create a list of dogs
    List<Mammal> mammals = dogs;   //reference it as a list of mammals
    List<Cats> cats = mammals;  // reference the mammals as a list of cats (!!?!)
    Cat tabby = new Cat();
    mammals.Add(tabby)   // adds a cat to a list of dogs (!!?!)
    

    However, if you do not need lists, just collections, (and you use C# 4 or later), you could define the dictionary as Dictionary<string, IEnumerable<Entity>>. WIth an IEnumerable it is not possible to add or modify the collection, just enumerate it, so any funny bussiness is by definition dissalowed. This is called Generic Type Covariance, and if you would like to read more on the subject, there are several great Eric Lippert blogs.

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

Sidebar

Related Questions

Suppose I have a Base class and a Child class that inherits from Base
In the project I'm working on now, we have base Entity class that looks
I have a Generic Base Class that I want to allow one of two
I have a class that inherits from a base class (this contains a lot
I have an abstract base class which inherits Sharp Arch's Entity class: /// <summary>
I have a base class that I want to look like this: class B
I have a simple base entity type called EntityBase that implements IEquatable<EntityBase>. I've used
Greetings! So I have a base class that defines a base64Binary property to be
I have a base class called Entity which has a static method called construct
I have a base class that has a virtual property: public virtual string Name

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.