A derived class inherits all public and protected members from the base
class. No cast is needed to access the members of the base class. Casting a
derived class to a base class type only serves to hide the members of the
derived class. The actual object's type will remain the same. Maybe it would
help to know what you are trying to do. In fact, for the example you gave
you don't even need that cast. Declaring prod as type Product is all you
need. You will not get a compiler error if you leave out the cast. Upcasts
can be implicit. Downcast must be explicit.
I would take a look at the sections in a good C# book that discuss casting
and the use of the is and as operators. This will help you understand things
better.
Thomas P. Skinner [MVP]
"Santi" <no*********************@gmail.com> wrote in message
news:cm**********@nsnmpen3-gest.nuria.telefonica-data.net...
I have two classes: Product and Fruit which inherits from Product.
If I try to narrow the reference to the base type by a cast, I always get
a
reference to the inherited type.
For example:
Fruit lemon = new Fruit();
Product prod = (Product)lemon; // Now prod is a Fruit Type!!
I want to get a Product reference from a Fruit object, is it possible?
thank you.