Cast to Extended Class in C# (dotnet 3.5<)

Posted by csharpnoob on Stack Overflow See other posts from Stack Overflow or by csharpnoob
Published on 2010-05-10T10:43:09Z Indexed on 2010/05/10 10:54 UTC
Read the original article Hit count: 666

Filed under:
|
|

Hi, i'm not sure if it's possible. I have a class car and a class mycar extended from class car. Class mycar has also a string list. Only difference.

How can i cast now any car object to a mycar object without assigning all attributes each by hand. Like:

Car car = new Car();

MyCar mcar = (MyCar) car;

or

MyCar mcar = new MyCar(car);

or however i can extend car with own variables and don't have to do always

Car car = new Car();
MyCar mcar = new MyCar();
mcar.name = car.name;
mcar.xyz = car.xyz;
...

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about casting