Boxing is copying the data

Asked

Viewed 129 times

7

Boxing is to transform value type in Reference type, right?

But when we copy a Ference type into another Reference type, it just copies the address and not the value. But when I convert int for object for example and copying to another object, is not working the same way.

Example:

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 10;
            object obj = x; //int vira reference type
            object obj2 = obj; //copia o endereço do obj pro obj2, certo?
            obj = 20; //modifica obj
            System.Console.WriteLine((int)obj2); //mostra obj2, era pra mostrar 20, nao? 
            //por que mostra 10?
            System.Console.ReadKey();
        }
    }
}

2 answers

7


Boxing creates a new instance of a type by reference. Boxing does not change the immutability of the type of data being stored. On the contrary, it guarantees. If you have a type for mutable value, which is rare, you cannot change its value while it is boxed. First you would have to unpack, change the desired limb and then box again.

What your code is doing is simply copying a value to a new object using a type by reference. You have a object which concretely carries a int, So even though it’s boxed in a type by reference, at the bottom it has an internal representation of a type by value. So the moment you copy obj for obj2 is copying your reference as expected. At this point the two variables point to the same object in the heap.

But this will change in future versions of the language.

You’re making two boxings in the code. When obj = x and then when it does obj = 20. Yes, you are boxing the value 20. 20 is a data of one type per value. It is not only variables that have type. Actually data have type, variables have the type of their value.

So you think that obj and obj2 are pointing to the same object, and are until you make a new one Boxing, then another object is created and that variable points to another address and they are independent.

If language didn’t work this way it would violate the immutability of the type.

  • So when I do obj = 20, does it create another int in the heap? And obj2 remains the "old int" address in the heap?

  • 2

    Exactly that. Because this is the function of Boxing. "Creates a new instance" is the key to understanding how it works. Note that today it is rarely necessary to Boxing in C#. This was important in C#1 before the Generic. It’s still in Java.

  • "Note que hoje raramente é necessário o boxing em C#" not to mention the performance drop that this type of operation can cause. :)

3

In a very simple way it is the following: Boxing does conversion of the VALUE that is in the stack to heap and obviously Unboxing is the opposite. So let’s take the steps . Net did:

  • int x = 10 // This is on the stack.
  • Object obj = x // creates an address in the HEAP and put the value 10
  • Object obj2 = obj; //At this time there are two "pointers" to the same address in the HEAP
  • obj = 20 // Here is the secret, because at this time is being held one in the new Boxing or this creating in the new address in memory HEAP for the object obj.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.