Type Casting

P

pitdog

This may not be the group to post this in. However I will try...

I am interested in how the .NET runtime actually handles casting. For
instance...

This will work if you assume ChildProduct is a derived class of
BaseProduct.

BaseProduct product= new ChildProduct();

However this will not work because you can't downcast.

ChildProduct = new BaseProduct();

I understand it conceptually. It will fail because there is no
guarantee that BaseProduct shares the same interface as ChildProduct.

However how does it actually check? Does it use reflection to look at
the public interfaces and make decision on that? I would really like to
know the details of how this works..

Any insight?

PD
 
B

Bruce Wood

This may not be the group to post this in. However I will try...

I am interested in how the .NET runtime actually handles casting. For
instance...

This will work if you assume ChildProduct is a derived class of
BaseProduct.

BaseProduct product= new ChildProduct();

However this will not work because you can't downcast.

ChildProduct = new BaseProduct();

I understand it conceptually. It will fail because there is no
guarantee that BaseProduct shares the same interface as ChildProduct.

No... it is because BaseProduct "is not a" ChildProduct. That is, since
ChildProduct is a class definition, not an interface definition, it
will fail *at compile time* because ChildProduct is not an ancestor of
BaseProduct in the inheritance tree.

Now, if you're talking about interfaces, something like this:

IFlappable foo = new BaseProduct();

Then the compiler will flag this as an error because BaseProduct does
not implement the interface IFlappable.

In neither case does the compiler need to delve into what methods,
properties, events, and fields the class defines. All it does is look
at the class hierarchy and what explicitly defined interfaces the class
implements (directly or indirectly via its ancestors).
However how does it actually check? Does it use reflection to look at
the public interfaces and make decision on that? I would really like to
know the details of how this works..

No reflection required. This is the compiler we're talking about here,
not the runtime. In fact, none of this has anything to do with casting,
as such.

Casting comes in when you tell the compiler that an assignment is
valid... when you make a promise, if you will. You could, for example,
say something like this:

ChildProduct myChild = (ChildProduct)myBase;

What you are saying here is that myBase, which _may_ contain a
ChildProduct (because ChildProduct is a child class of BaseProduct and
so is itself a valid BaseProduct that could be place in myBase), _is in
fact_ a ChildProduct. You're telling the compiler, "Trust me. I know
that this is a ChildProduct."

In this case, the compiler generates code to check that your promise is
indeed kept when the program runs. The process for verifying this at
runtime is the same as at compile time: the runtime uses, yes,
reflection, to examine the class hierarchy and which explicitly-defined
interfaces the classes implement to decide whether the actual object
that myBase references is, in fact, assignable to a reference variable
of type ChildProduct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top