.NET and C# interview questions – Boxing, Unboxing, Heap, Stack, By reference and by value.
Recently I went for a big IT multinational company and the interviewer grilled me in for a hour around the below concepts Boxing , Unboxing , Heap , Stack , By reference and by value. I am just putting my experience so that every one can be benefitted. The above 6 concepts are very much interlinked and asked by C# interviewers again and again. They can start from anyone of the three sections i.e. boxing / unboxing , Stack / Heap or By ref and value.
From which ever section they initiate the discussion, all interviewers touch base the above concept directly indirectly. In my particular C# interview he started first with…
What are stack’s and heaps ?
Stack are like series of compartment i.e. one above another. It keeps track of your running memory needed by your application. On the other hand heap is nothing but pile of objects which are kept one above another and they can be reached any moment of time.
I knew that I had answered right but he was not expecting that answer he was expecting something else….So bang came the next question.
So what kind of data stack has and heap has ?
Both stack and heap are memory types. Stack memory are value types like integer , double , Boolean etc , while heap memory are objects and string data types.
Once I answered the above question , I was expecting the next question on what are value types and references. But he went some other route but again near to expectation …
How is boxing and unboxing related to the above concept ?
Boxing happens when data moves from value type to reference type and unboxing happens when data moves from reference types to value types.
This leads to a small performance hit in the application.
I purposely put the last line so that he would probe further on performance and yes yes , he asked me the next question…Its my simple old trick to force the interviewer to ask question which i am good at..;-)
Why does performance decrease when boxing and unboxing happens ?
In boxing and unboxing data moves from value to reference and vice versa which needs quite a bit processing as they are different memory types all together. Due to the same there is a performance decrease when the boxing and unboxing phenomenon happens.
I knew this was becoming a in-depth discussion forum and the next question but obvious was …
So how can we avoid boxing and unboxing ?
We can not really avoid , but where ever possible we can do two things at least :-
- First is avoid unnecessary conversions.
- Second use generics to avoid the conversion situation.
What are the scenarios in which boxing and unboxing happens ?
One of the common situation is when we take data in to UI object like text boxes and we need to move the data in to a value data type like integer , double etc. As the UI textboxes and controls are objects its very difficult to avoid this scenario.
Are string by val or by ref ?
They are by ref. String and objects are by ref and they are always stored in a heap. All the other data types other than these 2 are value types.
This question was unexpected…but i had my concepts clear..so.
How do stack and heap memory gets claimed back when the application exits ?.
Stack is the running memory , the byval memory , So it just gets claimed when the application exits. The by ref memory or heap memory is not claimed as soon as the application exits , but its claimed by the garbage collector routine. So the GC just spawns and claims the memory acquired by the heap.
By Shivprasad Koirala.