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) {...} ...
source share