Is it really possible to call Child class constructor first instead of parent class constructor in case of inheritance in C#

StackOverflow https://stackoverflow.com/questions/23549585

Question

Can any body help to explain me that Is it really possible to call Child class constructor first instead of parent class constructor in case of inheritance in C#?

Was it helpful?

Solution

No, it is not possible to execute the code in the derived constructor before the constructor from the base class.

This usually only comes up as a problem if you're calling virtual methods from the constructors, something you shouldn't do.

OTHER TIPS

The .NET framework tries to guarantee that an object cannot be exposed to the outside world without all base-class constructors having been invoked. As a consequence, there are severe limits as to what a child-class constructor is allowed to before it calls the parent constructor. Child-class constructors written in C# will evaluate field initialization expressions before calling the parent constructor, so technically the child-class constructor is running before the parent. Unfortunately, at present, field-initialization expressions aren't allowed to do much.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top