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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:19:57+00:00 2026-05-27T09:19:57+00:00

I have three classes with common parent. Let’s say parent is Animal and children

  • 0

I have three classes with common parent. Let’s say parent is Animal and children are Dog,Cat and Parrot.

And I have one observable collection which contains collection of animals user is working with. Collection contains all animals of same type – user is either working only with all dogs or all cats or all parrots.

So I declared ObservableCollection<Animal> animals and depending on user choices I want to change contents of property animals to ObservableCollection<Dog> or ObservableCollection<Cat> or ObservableCollection<PArrot>. So it doesn’t matter if user currently works with dogs or cats but he can choose all actions animals have in common.

But it doesn’t work. It seems that I can’t assign ObservableCollection<Cat> to Property of type ObservableCollection<animal>. I would think it should work, because animal is supertype of cat, so I can assign cat to animal variable as usual.

Why can’t I do this and how can I solve this problem?

  • 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-27T09:19:58+00:00Added an answer on May 27, 2026 at 9:19 am

    One reason it doesn’t work: if it did work, you could have the nonsense scenario:

    ObservableCollection<Animal> animals = new ObservableCollection<Dog>();
    animals.Add(new Cat()); // cat is an animal, after all
    

    Another reason is that it is simply not the case that ObservableCollection<Dog> inherits ObservableCollection<Animal> – they are just different (parallel) closed generic types of ObservableCollection<>.

    What is permitted is that some interfaces with “out only” APIs (such as IEnumerable<T>) can be covariant, so if all you need is to iterate them, you can have:

    IEnumerable<Animal> animals = new ObservableCollection<Dog>();
    

    (presumably adding the dogs somewhere else)

    Another approach would be to use a non-generic API:

    IList animals = new ObservableCollection<Dog>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have 3 classes, in these classes I have 2 common function
I have three classes: PurchaseOrder, PurchaseOrderLine, Item PurchaseOrder children are PurchaseOrderLine which is related
Have three classes User, Group and Field. Many to many relationship on User /
I have three classes that I am using and have shortened for ease of
I have three classes, I am simply display my Facebook friends in tableView. And
I have three classes that all have a static function called 'create'. I would
I have three classes; Classes A and B both reference class C . How
I have three classes: ClassA , ClassB , ClassC . ClassC extends ClassB which
If I have three classes class A class B extends A class C extends
In my code I have three classes as follows: Forum , Forum::Thread and Forum::Post

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.