How exactly does this work?
If I have this base class
public class BaseClass
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public BaseClass SimpleClone()
{
BaseClass result = new BaseClass();
result.Value1 = this.Value1;
result.Value2 = this.Value2;
return result;
}
}
And this child class
public class DerivedClass : BaseClass
{
public bool Value3 { get; set; }
public DerivedClass()
{
Value3 = true;
}
}
How can I downcast BaseCast.SimpleClone()
into anotherObj
? What would happen to Value3?
While knowing what happens is good, I am also interested in why it works this way.
If I understand correctly your question is What happens when you do the following
DerivedClass derived = (DerivedClass)baseObj.SimpleClone();
Did you try that? Simply it will result in InvalidCastException
since BaseClass
is not DerivedClass
.
I have answered a similar question here, That should clear things up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With