Quote:
Posted by CornedBee
At pos 4, a has been assigned the value of b.
This means that both a and b now point to X,
W has been abandoned and will eventually be garbage collected.
a can point to X because MySubclass is a subclass of MyClass.
The downcast on the reference is performed automatically.
This has absolutely no effect on X itself, though.
What i don't understand is why the downcast is implicit. When assigning smaller type to a larger type the cast is always implicit. Visa versa cast mandatory.
Quote:
Posted by CornedBee
At pos 5, it has been attempted to assign the value of a back to b.
However, a is a ref to MyClass, b to MySubclass. An upcast cannot be performed implicitely, you must explicitely cast it:
b = (MySubclass)a;
Now you said that both a and b now point to x(which is MySubclass) so