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

質問

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

役に立ちましたか?

解決

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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top