开发者

Implicit conversion in type declaration? What is object type that is stored on the heap?

Ok imagine I have a base class BaseClass, as well as a child class ChildClassA which derives from BaseClass.

What happens when I do this?

BaseClass b = new ChildClassA;

What I imagine is happening i开发者_如何学Cs that:

  1. ChildClassA gets created and stored on the heap as a ChildClassA type!
  2. The variable b gets assigned a reference to the ChildClassA
  3. There is an implicit conversion from the ChildClassA to BaseClass
  4. But the object is still stored in the heap as a ChildClassA

The reason I ask is that as I understand it, once an object is declared and stored on the heap, that's what it ALWAYS is. A conversion just tells the CLR to treat it like it's a different type, but it really always still is the original type, and knows it's still the original type.

Am I correct? Anything I am missing here?


You're spot on with your understanding.

b refers to the ChildClassA instance. The latter is still on the heap as it was previously and no new objects are heapificated as a result of the assignment of the instance to BaseClass b.


By Jove, you've got it!

A conversion just tells the CLR to treat it like it's a different type,

Actually it's not a conversion, more like an interpretation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜