Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boxing vs Unboxing

Another recent C# interview question I had was if I knew what Boxing and Unboxing is. I explained that value types are on Stack and reference types on Heap. When a value is cast to a reference type, we call it boxing and vice versa.

Then he asked me to calculate this:

int i = 20; object j = i; j = 50; 

What is i?

I messed it up and said 50, where its actually 20. Now I think understand it why, however when I was playing with different combinations I was surprised to see this:

Object a = 1; // Boxing Object b = a; // referencing the pointer on stack to both objects on heap a = 2; // Boxing 

I was expecting to see b == 2 as well, but it isn't, why? Is it because the second boxing destroys and replaces the whole a object on the heap?

Because if I do this, it's fine:

public class TT {     public int x; }  TT t = new TT(); t.x = 1; TT t2 = new TT(); t2.x = 2; t = t2; t.x = 3; 

What is t2.x? It should be 3, and it is. But this is not an example of boxing / unboxing at all, is this correct? So how would you summarize this?

Could the values ever become the same in a boxing/unboxing conversion as above?

like image 883
Houman Avatar asked Apr 18 '09 21:04

Houman


People also ask

What is the difference between boxing and unboxing?

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the common language runtime (CLR) boxes a value type, it wraps the value inside a System. Object instance and stores it on the managed heap. Unboxing extracts the value type from the object.

Which is faster boxing or unboxing?

If I look up what unboxing and boxing does you see that the difference is that boxing allocates memory on the heap and unboxing moves a value-type variable to the stack. Accesing the stack is faster than the heap and therefore unboxing is in your case faster.

What is boxing and unboxing programming?

Boxing and unboxing are the processes that enable value types (e.g., integers) to be treated as reference types (objects). The value is “boxed” inside an Object and subsequently “unboxed” back to a value type. It is this process that allowed you to call the ToString( ) method on the integer in Example 6-4.

Why boxing and unboxing is required?

Boxing and unboxing enables a unified view of the type system wherein a value of any type can ultimately be treated as an object. With Boxing and unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to and from type object.


1 Answers

Very short: boxing means creating a new instance of a reference type. If you know this, you understand that one instance does not change by creating another.

What you are doing with a = 2 is not changing the value in the 'box', you are creating a new instance of a reference type. So why should anything else change?

like image 194
Stefan Steinegger Avatar answered Oct 12 '22 02:10

Stefan Steinegger