Closed Xyncgas closed 3 years ago
The string "2B" is still reachable, as shown in the sample code below.
ClassA obj = TestSample();
Console.WriteLine(obj.a); // "A2"
ClassB castObj = (ClassB)obj; // this cast is validated at runtime
Console.WriteLine(castObj.b); // "2B"
Since the runtime knows the actual type of the backing object, and since the caller might cast the object back to the more derived type, the entire object graph is kept alive.
thanks for the explainations @GrabYourPitchforks
In the example :
So, at {7ED6CC5A-15F9-49F7-A762-C8B3B0B7A7F2}, I have two questions : I_Have_A_Pineapple is now ClassA but does the standard say it's actually still ClassB or I_Have_A_Pineapple.GetType()==typeof(ClassB) ?
I_Have_A_Pineapple now doesn't have access to string b which value is "2B", because it's casted to ClassA and 2B only existed as Class B member, so a while later would gc be collecting this reference and 2B is gone forever while it might only exist at this moment?