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 is that:
- ChildClassA gets created and stored on the heap as a
ChildClassAtype! - The variable
bgets assigned a reference to the ChildClassA - There is an implicit conversion from the ChildClassA to BaseClass
- 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.
brefers to theChildClassAinstance. 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 toBaseClass b.