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