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

Domanda

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#?

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top