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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:26:33+00:00 2026-05-13T16:26:33+00:00

I got a class X and a class Y, the latter which derives from

  • 0

I got a class X and a class Y, the latter which derives from X :

class x {}
class y : x {}

Then somewhere I am using a list of X :

List<X> lstX;
...

Then I’d like to use a new list of Y, from the data in my other list…something along those lines :

List<Y> lstY = lstX;

I would believe that the items in the list of X would get converted automatically into Y, but thats not the case.

Also, how could I initialize a new instance of Y, from a certain X? I would like to do :

var newX = new X();
var newY = new Y(X);

but it does not seem to work like that.

Thanks for your help!
and sorry for formatting, trying my best

  • 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-13T16:26:33+00:00Added an answer on May 13, 2026 at 4:26 pm

    There are a couple of questions here.

    First: “I can assign an object of type Tiger to a variable of type Animal. Why can I not assign an object of type List of Tiger to a variable of type List of Animal?”

    Because then this happens:

    List<Tiger> tigers = new List<Tiger>();
    List<Animal> animals = tigers; // this is illegal because if we allow it...
    animals.Add(new Giraffe()); // ... then you just put a giraffe into a list of tigers.
    

    In C# 4 it will be legal to do this:

    IEnumerable<Tiger> tigers = new List<Tiger>();
    IEnumerable<Animal> animals = tigers;
    

    This is legal because IEnumerable<T> has no “Add” method and therefore this is guaranteed to be safe.

    See my series of articles on covariance for details about this new feature of C# 4.

    http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

    Second question: “how could I initialize a new instance of Tiger, from a certain Animal?”

    You cannot. The Animal in question could be a Ladybug. How are you going to initialize a new Tiger from an instance of Ladybug? That doesn’t make any sense, so we don’t let you do it. If you want to write your own special method that knows how to turn arbitrary animals into tigers, you are free to do so. But we don’t know how to do that for you.

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

Sidebar

Related Questions

I've got a class somewhat like this: public class Test { private final List<ISomeType>
Question about subclassing in matlab, under the new class system. I've got class A
I've got a class that I'm using as a settings class that is serialized
Say I've got a class like this: class Test { int x; SomeClass s;
I've got a class that looks something like this: public class Parent { public
I've got a class like this: [XmlRoot(channel)] public class Channel { [XmlElement(title)] public String
Say I've got a mock setup like this: JUnit4Mockery context = new JUnit4Mockery(); MyInterface
I've got a MenuManager class to which each module can add a key and
I've got a problem with my code. It basically looks like this: someclass.hpp: class
I got two classes User and Role which are mapped to each other using

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.