C# Constructor Execution order Parent Child | C# Interview Questions | Csharp Interview Question

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning good afternoon good evening namaste salaamwalekum welcome to quest pond's youtube channel and what are we learning today today we will be discussing about a very controversial she sharp interview question it's controversial but the answer is very simple but also it has his own twists and turks right so this she sharp interview question goes something like this in a parent-child relationship so if you have a parent-child relationship which constructor will fire first so for example you can see that we have a class human here so from class human we have class man and class woman who are inheriting so if the woman has a constructor here so which will fire first so let us say if i create an instance of a woman object here will the human constructor fire first or will the woman construct her fire first right so i repeat the question in a parent-child relationship how is the execution order of the constructor right and uh before i start here i would like to just inform you please go ahead and visit questpond.com where i've recorded hoods and hordes of interview questions so we have shisha interview questions then we have angular interview questions then we have asp.net interview questions and then we have msbi and sql server so it keeps going on so i would suggest please go ahead and visit quespond.com and enjoy this these videos right my first suggestion is do not try to memorize it or do not try to buy heart it by heart yes by heart that word is very harsh i'm using here but that's a bitter truth what candidates do in she shop interviews when you buy heart something you know there are big pauses in the interview you keep glaring for seconds looking at the top wall you cannot confidently counter cross questions and so on and obviously obviously this sends a very very bad message to the interviewer so do not try to memorize things try to think logically understand things clear your concept so that you can handle the situation better in the interview so let's try to think logically here so you can see that we have a class human here and from this class human a man is inheriting and a woman is inheriting right so if i create an instance of a woman object or if i create a instance of a man object right obviously the first thing is the human object has to get created right the parent has to get instantiated isn't it on the base of the parent the child will stand so it is very logically logical that first the parent constructor should fire first right first the human object should get instantiated and on that base the man or a woman object should get instantiated right so definitely you know in a parent-child relationship if you have constructor in the parent and the child logically the parent constructor should fire first and that's what exactly happened so if i put a debug point here and let us say if i put a debug point here right so you can see here i'm instantiating the woman object here right so what happens now when i run this you will see that the first thing is it will instantiate it will run the constructor of the parent so there it is it goes here you can see now first it is running the constructor of the parent you can see console.writeline human is getting executed and after that the child constructor fires right so when it comes to constructor first the parent constructor fires and then the child constructor fires so just try to remember logically first the parent has to get instantiated and on that base the child has to get instantiated but now coming to the twist part there are some naughty interviewers they don't stop right they don't stop so these naughty interviewers you know can cross question you what about the initializers initializers means what if you have uh member level variables like this you can see in the class human i have put public int i equal to 0 and in case of class woman i have put public int i1 equal to 0. so what about the initializers now the initializers are little bit different initializers if you see it i'm going to go and run this let us watch so first what happens is the child initializers run look at this my the child initializer runs first so the child initializer runs then the parent initializers run you can see now the parent initializers are running then the parent constructor runs and then the child constructor runs right so 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 right so the whole sequence is like this first the child initializer will fire then the parent initializer will fire then the parent constructor code will fire and then the child constructor code will fire so 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 right now you'd say that okay shave double standard double standard caught you you said that start thinking logically so you said that okay like 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 right so i will leave that as a homework you know go ahead google it why do the initializers fire in an opposite logical sequence right i will give you a hint here what if the parent goes and tries to call the child objects and what if those objects are read only so i'm giving an uh indication also that why the initializers fire in the opposite direction right and very very quickly before i end the session if you go ahead and if you share this video on your linkedin or on your facebook or on your twitter you would get a free interview question pdf a free shop interview question pdf so go ahead and share this video happy job hunting happy learning thank you
Info
Channel: Questpond
Views: 8,460
Rating: undefined out of 5
Keywords: c# interview questions and answers, c# interview, c# interview questions, c# constructor sequence, C# constructor parent child, c# constructor fires first, c# tutorial, c# programming, c#, c# interview questions and answers for experienced, .net interview questions, c# interview questions and answers for freshers, c sharp interview questions, c sharp interview questions and answers, csharp constructor chaining, c# constructor chaining, csharp interview for freshers
Id: O5VBIx5uslA
Channel Id: undefined
Length: 6min 45sec (405 seconds)
Published: Sat Jun 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.