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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:06:14+00:00 2026-05-28T15:06:14+00:00

As I don’t know how my problem is called, I cannot guarantee, that nobody

  • 0

As I don’t know how my problem is called, I cannot guarantee, that nobody has asked the same question recently or at all.

I did notice, however that there are quite a few threads with a similar title, but they don’t seem to be relevant to my problem.

I have a custom list class, that implements Generics.

class MyList<T>
{
    public void add(T item) // adds an item to the list
    { /* code */ }
    public void add(MyList<T> list) // attaches an existing list to the end of the current one
    { /* code */ }
}

I also have the classes:

class Apple : Fruit

and

class Banana : Fruit

Now, comes the relevant code:

MyList<Fruit> fruitList = new MyList<Fruit>();
// fill fruitList

fruitList.add(new Apple()); // works, of course
fruitList.add(new Banana()); // works as well, of course

MyList<Apple> appleList = new MyList<Apple>();
// fill appleList

fruitList.add(appleList); // doesn't work. Why?

Even though appleList is a MyList(Of Apple) and Apple is Fruit, VisualStudio doesn’t accept MyList(Of Apple) as argument, when MyList(Of Fruit) is asked.

However, if I were to declare the list like this:

MyList<object> fruitList = new MyList<object>();

Then everything works again. What exactly did I do wrong?

An answer would be much appreciated, and thank you for taking the time to read, even without answering.

  • 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-28T15:06:15+00:00Added an answer on May 28, 2026 at 3:06 pm

    You’re trying to use covariance.
    .Net only supports generic variance on interfaces, so that won’t work.

    In addition, covariance only makes sense on immutable types.
    Had it been possible to convert a MyList<Apple> to a MyList<Fruit>, you would then be able to add an Orange to the list, violating type safety.

    Instead, you can make the method generic:

    public void Add<U>(IList<U> list) where U : T
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know if this has been asked before, so point me to another question
I don't know if this has been asked before, but what i'd like to
Don't know if I worded the question right, but basically what I want to
I don't know if this question is trivial or not. But after a couple
Don't know if this is an eclipse specific problem but whenever I declare a
Don't worry, I'm not going to ask that question, yet again... I am wanting
Don't know if this has been answered before. Have custom routes to users. If
Don't yell at me! i have seen many threads claiming that HTML cannot be
Don't know if there is a better way to do this, so that is
Don't let below code scare you away . The question is really simple, only

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.