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
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
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.
That is never going to work; after all List<Y> lstY = lstX;
just copies the reference (unless you add your own implicit static conversion operator to your own list type) - so it is still a list of X
and could contain things other than Y
instances.
Even in 4.0, co/contra variance doesn't extend to a: lists (both in
and out
), or b: concrete types (like List<T>
).
Interestingly though, it will (and always has) work for arrays of reference types, but only in the direction X[] arrX = arrY;
. It doesn't convert anything; if you try and put the wrong data in it'll throw an exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With