Boxing & Unboxing in C#

Questpond
Posted by in C# category on for Intermediate level | Points: 250 | Views : 1355 red flag
Rating: 4 out of 5  
 1 vote(s)

In this article, we will learn about Boxing and Unboxing in C#.
Recommendation
Read Casting in C# before this article.


Introduction
Boxing
Unboxing
Performance implication

Introduction

Boxing and unboxing are one of those concepts which every .NET developer should understand. Lot of time knowingly and unknowingly we implement these concepts and it can have disastrous performance issues. So in this article we see what is boxing and unboxing and why does it cause performance issues and how to use them carefully.

Boxing

So let us start with boxing and below are some points and definitions around it.

  • Boxing is a process of converting value type to the referenced type.
  • Value type is stored in Stack Memory
  • Referenced Type is stored in Heap Memory.
  • Let us understand the above statement in detail.

    Below is a simple console application code which does Boxing.

    static void Main(string[] args)
    {
       int i = 10; // Step 1 Declar/Initialisation of Variable i of integer datatype.
       object o; // Step 2 :- Declaration of Object o.
       o = i; // ß Step 3 :- Boxing happens here .Assigning value of i to object o.
    }
    

    When the above programs runs below are the sequence of events :-

  • First the variable ‘i’ is assigned 10 value and it is stored on Stack memory.
  • Then object reference ‘o’ is stored on the Heap.
  • In the third step boxing happens where value type is moved to the object and in this case the value is assigned on heap.
  • Unboxing

  • Unboxing is a process of converting reference type to the value type.
  • Let us understand the above statement in detail.

    Below is a simple code which does Unboxing.

    static void Main(string[] args)
    {
       int i = 10; // Declaration and Initialisation of Variable i of integer datatype.
       object o; // Declaration of Object o.
       o = i; // Boxing
       int j = (int)o; // Unboxing
    }
    

    During Unboxing the referenced type residing on Heap Memory is copied to Stack Memory as a value type. Also Unboxing checks the referenced type to make sure that it is a boxed value of the given value type. It means before unboxing ‘o’ it will check whether ‘o’ is a referenced type and is pointing to a value in heap memory.

    Performance implication

    So now that we have understood the definition of boxing and unboxing, the next thing is to understand what is the implication of boxing and unboxing. Boxing and unboxing causes your variables to jump from one memory type (value type) to other memory type (reference type) which can further lead to performance issues.

    So in simple word, we should try to avoid boxing and unboxing as much as we can.

    But then there are situations where we can not avoid it. For example see the below code. We are taking data from the user interface (web page)I. When we receive data from user interface it comes in the form of string so we need to unbox to convert it into the data type we want. And also when we set data the data to user interface objects like TextBox, we need to convert to string.

    int I = Convert.ToInt(textbox1.text);

    Below is video which explains the concept of boxing and unboxing and it also demonstrates a small performance testing highlighting the performance implications of boxing and unboxing.

    Recommendation
    Read Event accessors and Lock statements in C# after this article.
    Page copy protected against web site content infringement by Copyscape

    Login to vote for this post.

    Comments or Responses

    Posted by: Raja on: 12/4/2015 | Points: 25
    Good post Shivprasad, the video along with article on this topic helped me a lot in understanding this topic.

    Please keep posting these types of posts and video.
    Posted by: Rajeshatkiit on: 12/17/2015 | Points: 25
    You can refer for more info.
    http://www.c-sharpcorner.com/UploadFile/2072a9/boxing-and-unboxing-in-C-Sharp/

    Login to post response

    Comment using Facebook(Author doesn't get notification)