Home > Software > .NET > ASP.NET
Interview Questions   Tutorials   Discussions   

ASP.NET - What is boxing and unboxing ?

asked mar October 4, 2014 03:00 AM  

What is boxing and unboxing ?


1 Answers

answered By siddhant   0  


int Val = 1;
Object Obj = Val; //Boxing

The first line we created a Value Type Val and assigned a value to Val. The second line , we created an instance of Object Obj and assign the value of Val to Obj. From the above operation (Object Obj = i ) we saw converting a value of a Value Type into a value of a corresponding Reference Type . These types of operation is called Boxing.


      int Val = 1;
  Object Obj = Val; //Boxing
  int i = (int)Obj; //Unboxing

The first two line shows how to Box a Value Type . The next line (int i = (int) Obj) shows extracts the Value Type from the Object . That is converting a value of a Reference Type into a value of a Value Type. This operation is called UnBoxing.

Boxing and UnBoxing are computationally expensive processes. When a value type is boxed, an entirely new object must be allocated and constructed , also the cast required for UnBoxing is also expensive computationally.

   add comment

Your answer

Join with account you already have



 Write A Tutorials
Online-Classroom Classes

  1 person following this question

  Question tags

asp.net × 1

Asked 1 year and 1 month ago ago
Number of Views -107
Number of Answers -1
Last updated
2 years and 17 days ago ago

  Similar questions

Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!