Boxing vs. ValueType by reference; Who cares?

There were a few recent questions about ValueType boxing as an object, in particular, whether this happened in some cases.

Something that I understood, I don’t know what is the difference between β€œboxing” ValueType (considering it as a reference object) and simply referring to it by reference, for example, using the ref or out keywords (where you transmit Is it just a "pointer")? In both cases, the value is somewhere where you can point to it (for an object it is a bunch, for a local ValueType value it is ... where exactly?).

If I had to guess from what I know about C ++, I would say that it works as follows: the ValueType referenced by the link (say, via the parameter keyword) remains at the level of the call stack, for which it is limited, but a shortcut pointer to this variable bucket on the stack is created and becomes part of the next stack layer. Since the value is already stored in memory (possibly even in the processor cache), you do not need to create something new on the heap; the only new thing is the pointer, which is its own ValueType (IntPtr) and is stored on the stack itself, so AFAIK will be faster than putting something in a heap.

Is this what is happening, or is there something else?

EDIT: More clarity:

public void TakesAnObject(Object obj) {...} public void TakesAnIntValueType(ref int myValue) {...} public void AnotherIntParameterMethod(out int myValue) {...} ... //this locally-scoped variable is simply created on the stack. int myInt = 5; //Performs boxing; an Object is instantiated in the heap that holds the //variable value from the stack, and that is passed by ref. TakesAnObject(myInt); //Apparently does NOT perform boxing, but we're still dealing with a reference. //So what going on? TakesAnIntValueType(myInt); //Again created on the stack, with the default 0. int anotherInt; //Again, apparently no boxing, but we're dealing with a reference to anotherInt. AnotherIntParameterMethod(anotherInt); 
+4
source share
3 answers

Class references can be freely copied and allowed to exist indefinitely. They can be considered as identifiers of objects that are stored (always) as autonomous elements on the heap. To avoid the abuse of the term link, I like to think of them as object identifiers.

When a routine takes a parameter by reference, that reference is a special type of thing that is outside the normal class system (I will call it ParameterReference). Unlike the ObjectID, which can exist indefinitely, the ParameterReference parameter is allowed only for the duration of the called function. In addition, unlike the ObjectID, which always contains a link to a separate object, ParameterReference contains a link to a local variable on the stack, a field inside the Object class, an element inside an array, or a field inside a structure that itself corresponds to one of these descriptions. If ParameterReference points to a local variable, this variable will cease to exist after it leaves the scope; Attempting to use the ParameterReference parameter after this time may result in data corruption. Since the scope of the variable is guaranteed to expand, at least until the called procedure is called, and since the ParameterReference parameter ceases to exist at that time, there is no danger of ParameterReference accessing the variable that no longer exists.

+3
source

TakesAnObject(myInt); must be a field because it is obtained as an object type (reference type). If the target was a value type, it would be copied.

TakesAnIntValueType(ref myInt); is a reference to the same memory area as myInt, so when you change "both changes". If not for ref, the value would be copied instead.

+1
source

You're close When a value type is placed in a box, it is copied to the structure of the object that lives on the GC heap. The structure of an object has the usual preamble for objects, and the bits that come after that are a structure of type blitted value. When it is unpacked, this structure is repeatedly written on a stack.

-1
source

Source: https://habr.com/ru/post/1337175/


All Articles