Boxing and Unboxing - C# / VB.NET ?

Posted on 2012-08-27
Last Modified: 2012-08-28

What will happen when there is Boxing occurs ? And when there is Unboxing occurs ?


Boxing: Stack value is written to Heap ???

Unboxing: Heap value is written to Stack ???

Please do let me know how that works with respect to memory allocations.

Question by:milani_lucie
    LVL 43

    Assisted Solution

    I think you have said it pretty much correctly (or at least how I understand it).  

    Boxing will convert a value type into a reference type (for garbage collection heap) and the unboxing releases a reference type into the value type of object.
    LVL 43

    Expert Comment

    LVL 29

    Accepted Solution

    Yes, it happens exactly as you described:
    Boxing converts value type to reference type , thus allocating memory on Heap . Unboxing converts already boxed reference types to value types through explicit casting , thus allocating memory on stack .
    And one more reference:

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now