Memory Organization
To understand boxing and unboxing we should understand computer memory, how it works, and how C# compiler (.NET) uses computer memory to store data.
Operating Systems and CLR (Common Language Runtime) divides computer memory into two parts called stack and heap to work with data. All the value types are stored on stack and all the reference types are stored on heap. Both parts are managed in a distinct manner.
- Whenever we call a method all the local variables are created on stack and when we call another method or return to the Main method all the acquired memory is released from the stack to reuse. This is done by the .NETGC (Garbage Collector) automatically. For example:


Inside addCount method local variable Count is incremented by one. But in the main method count the variable has a value of 0 because after executing the addCount method all the memory taken by its local variable was released and no Count variable exists anymore.

- When we create an instance of any class (custom class or system class) it is created on heap. We use new keyword to create all the reference types. Its reference is stored on the stack that points to a specific location on the heap. We can create many objects as per requirement they all will point to the same location on heap. And when last reference is removed all the previous object references are also destroyed and the memory on heap is released (although this is not done immediately because like stack this is not done using .NET CG). That’s why heap is used for reusability whenever working with objects or reference types. For example:


Because array is a reference type when we call addValues method local variable array was not removed from memory both variables in main and in addValues method reference to the same memory location on the heap.



















Asfend YarPosted Feb 15, 2016, 2:03 PM
Try to share such kind of stuff ...
Kumaresh RajalingamPosted Feb 13, 2016, 12:34 AM
Nice share
Vignesh ManiPosted Feb 11, 2016, 4:49 PM
Nice one
Mohammed IbrahimPosted Feb 11, 2016, 1:17 PM
nice
Namrah KhurramPosted Feb 11, 2016, 12:10 PM
Nice read! Boxing and Unboxing explained really well with illustrations and code snippets
Jaco ZwartsPosted Feb 11, 2016, 12:00 PM
Nice Article really well done
Raja TPosted Feb 11, 2016, 8:19 AM
Nice, Thanks for sharing
Rupesh KahanePosted Feb 11, 2016, 8:17 AM
Nice share
Amit KumarPosted Feb 11, 2016, 7:28 AM
nice
Anil Kumar MurmuPosted Feb 11, 2016, 5:38 AM
Thanks for sharing
Shubham KumarPosted Feb 11, 2016, 2:25 AM
too informative keep share
Sibeesh VenuPosted Feb 11, 2016, 1:51 AM
Nice Share
Sujeet SumanPosted Feb 10, 2016, 11:57 PM
Nice...
Sr KarthigaPosted Feb 10, 2016, 11:21 PM
Good one
Jaipal ReddyPosted Feb 10, 2016, 11:17 PM
nice explanation
Humayun Kabir MamunPosted Feb 10, 2016, 10:55 PM
Nice...
Pankaj Kumar ChoudharyPosted Feb 10, 2016, 8:41 PM
Really Nice Explained Article and some new things for me thanks for share.......
Mohammed IbrahimPosted Feb 10, 2016, 7:50 PM
nice
Vignesh ManiPosted Feb 10, 2016, 4:34 PM
Nice one
Ehsan SajjadPosted Feb 10, 2016, 2:22 PM
It is not necessary that value type stored on Stack , Value Types can also be in heap, it's misconception of programmers
Santhakumar MunuswamyPosted Feb 10, 2016, 2:08 PM
Thanks for nice article