Where can we use BOXING and UNBOXING concept ?

Showing Answers 1 - 6 of 6 Answers

mudduswamy

  • Dec 23rd, 2006
 

In boxing and unboxing is helpful for developers.Actually the boxing is used to convert the value type into object type.boxing is used many apllication such as wndows applicaton,console application and C#.

  Was this answer useful?  Yes

Guest

  • Jan 2nd, 2007
 

Hi ....

Sorry to say, but I have doubt on your statement. There is no such a thing like "useful" in boxing/un-boxing. Basically its the process which is done by CLR(Remember - Runtime) when "some" type of conversion is required.

What is Boxing?
Its "Box" + "ing". So, in other words, its "some" kind of process which creates boxes. Here, BOX MEANS OBJECT.

Why CLR needs to create BOX(object)?
Becuase, when we want to assign the "value-type" variable's value(R-Value) to referebce type object(L-Value), at that time CLR creates (implicitly) the BOX(object) of value type. Let's see pretty simple example of Boxing.

Example: (Code in C#)

     int i = 10; // i is the type of "value type"
     string s: // s is the type of "reference type"
     s = i.ToString(); // i.ToString() - RValue does Boxing... 
                           //...and assign the string object to "s"(LValue)

What is Un-Boxing?
Its the process of creating value type "value"(R-Value) from reference-type(object), and assigning to the variable of "Value Type".

Example: (Code in C#)
     int i = 5;
    
object obj = i; //BOXING
     
int j = (int)obj; //UN-BOXING

Note: In most of scenarios, Boxing seems to be implicit action(By CLR) and Un-Boxing seems to be explicit action(By developer by implying casting.)

Please let me know your comments.

Happy Programming....
techy

   

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions