Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# polymorphism simple question

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

like image 924
Bruno Avatar asked Feb 09 '10 19:02

Bruno


People also ask

What C is used for?

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 ...

Is C language easy?

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 in C language?

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.

What is the full name of C?

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.


2 Answers

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.

like image 160
Eric Lippert Avatar answered Oct 24 '22 21:10

Eric Lippert


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.

like image 31
Marc Gravell Avatar answered Oct 24 '22 19:10

Marc Gravell