Welcome to Questpond s youtube channel. Today we will be discussing about very
controversial C# Interview Question. It is controversial but the answer is very simple,
also it has its own twist and talks. This C# Interview Question goes something like this.
If you have a parent-child relationship which constructor will fire first. For example,
we have a class Human. From class Human, we have class Man and class Women who are inheriting.
If the women have the constructor which will fire first? If I create instance of Women object
will the Human constructor fire first or will the Women constructor fire first?
In a parent-child relationship, how is the execution order of the Constructor?
Before I start, would like to just inform you visit questpond.com where I have recorded
hoards and hoards of interview questions. We have C# interview questions,
Angular interview questions, ASP.NET Interview questions,
MSBI, and SQL Server so keeps going on. Visit questpond.com and enjoy these videos.
Do not try to memorized or byheart it. Byheart that word is very harsh I using here but that s a
bitter truth what candidates do in C# interviews. When you byheart something there are big pauses
in the interview, you keep glaring for seconds looking at the top wall you cannot confidently
counter across questions and obviously this sends a very very bad message to the interviewer.
Do not try to memorized things try to think logically understand things clear your concept
so that you can handle the situation better in the interview.
Let s try to think logically here, we have class Human here and from this class Human, a man
and women is inheriting. If I create a instance of a Women object or create a instance of a Man
object obviously the first thing is the Human object has to get created.
The parent has to get instantiated. On the base of the parent, the child will stand.
It is very logical that first, the parent Constructor should fire first. First,
the Human object should get instantiated and on that base the Man or a Women object should get
instantiated. In a parent-child relationship if you have Constructor in the parent and the child
logically the parent Constructor should fire first and that is what exactly happens. If I put a debug
point here and a debug point here means it is instantiating the Women object here, when I run
this, you will see that the first thing is it will run the Constructor of the parent. First,
it is running the constructor of the parent i.e. Console.WriteLine( Human ) is getting executed.
After that the child Constructor fires. When it comes to Constructor first the parent Constructor
fires and then the child Constructor fires. Try to remember logically first the parent
has to get instantiated and on that base, the child has to get instantiated.
Now coming to the twist part there are some naughty interviewers they don t stop.
These naughty interviewers you know can cross-question you what about the Initializers.
Initializers means what if you have member level variables like this you can see in the
class Human I have put public int i = 0; and in case class Human I have put public int i1 = 0;
Initializers are little bit different, will run this lets a watch.
First the child initializers runs then the parent initializers run then the parent constructor runs
and then the child constructor runs. When it comes to Constructor,
it is always first the parent and then the child when it comes to Initializers it is always
first the child and then the parent. The whole sequences like this first
the child initializers will fire then the parent initializers will fire then
the parent Constructor code will fire and then the child Constructor code will fire.
Let us summarize the initializers go in the opposite direction, the Initializers first
fire from the child and then the parent while when it comes to the Constructor code you know they go
in a logical sequence first the parent Constructor code fires, and then the child.
You would say that Shiv double standard caught you,
you said start thinking logically first the parent gets created and on the top of it the
child instance gets created and now why are the Initializers running in the opposite direction.
I will leave that as a homework, google it why do the Initializers fire in a opposite
logical sequence. Will give you a hint here what if the parent tries to call to the child objects
and what if those objects are read-only. I am giving an indication also that why the
Initializers fire in the opposite direction right. Before I end the session if you share this video
on your LinkedIn or on your Facebook or on your Twitter you would get a C# Interview
Questions PDF. Share this video Happy Job Hunting! Happy Learning! Thank You!