Home > Software > .NET > Dot-Net
Interview Questions   Tutorials   Discussions   

Dot-Net - What is boxing and unboxing?

asked mar October 4, 2014 06: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

  1 person following this tag

  Question tags

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

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!