Theoretically, why can't languages offer this child class to parent class feature?

by JᴀʏMᴇᴇ   Last Updated January 12, 2018 17:05 PM

Consider the following:

public class MyParentClass {
   public int MyInt { get; set; }
}

public class MyChildClass : MyParentClass
{
}

public class AnotherClass
{
   public MyChildClass GetChildClassFromParentClass(MyParentClass parent)
   {
       return new MyChildClass() { MyInt = parent.MyInt };
   }
}

Since MyParentClass and MyChildClass share this 'relationship', I'm wondering why it's not possible to clone the class into it's child without the manual step of copying values across. Maybe we can't guarantee that MyChildClass has dependencies being injected which causes a problem, but then can't the language feature permit this if there is a parameterless constructor defined then?

Let me be clear I know I'm wrong here, there definitely are scenarios which make this a language feature that isn't feasible or safe, I'd like to know what those scenarios are.

NOTE:

I don't want an opinion, this shouldn't be an opinion-based discussion. There are reasons this isn't a feature.

Tags : c# .net


Related Questions



Cache file /home/queryxchang/questarter.com/apps/frontend/config/../cache/-q-theoretically-why-can-39-t-languages-offer-this-child-class-to-parent-class-feature-6-363901-html could not be written