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#?
2 Answers
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.

- 1
- 1

- 380,855
- 102
- 628
- 825
-
I sometimes wonder what the costs and benefits would have be of having future Frameworks' versions of `Object` include a `protected virtual NotifyOfConstruction(Exception failureIfAny)` method which would be invoked immediately before the most-derived constructor returns control to client code? Constructing an object often requires exposing it to other objects before it's exposed to client code, and that in turn raises the possibility of its virtual methods being called. Letting base objects give derived objects a chance to construct themselves before such construction would ease that. – supercat May 08 '14 at 19:15
-
I think by virtual method it is also not achieved as mostly people said by the way your mean is that it is not possible in real scenario right ? – Rehan May 12 '14 at 05:13
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.

- 77,689
- 9
- 166
- 211