c# - How can I initialize an instance of a derived class from an instance of the base class? -
i've looked @ how can create instance of derived class instance of base class , include private fields? , create instance of derived class base class either i'm not getting it, or they're not quite i'm looking for.
i have class, derived another, implementing particular interface:
public class derivedclass : baseclass, iderivedclass
baseclass
implements ibaseclass
, iderivedclass
inherits ibaseclass
.
sometimes, want able initialize derived class, when have instance of base class. create constructor copies each of properties instance of base properties of instance of derived class, there way? along lines of:
baseclass bc = new baseclass(); derivedclass dc = new derivedclass() : bc;
is do-able? if not, why not? i'm prepared accept might not possible, if so, i'd quite interested if can explain why.
no, not possible.
you have create object since has have characteristics of second object. can't copy 1 another. might object invalid state if allowed.
the best thing create 'copy constructor', mentioned.
Comments
Post a Comment