In C#, according to the docmentation,
“Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap.”
What happens if there is no “garbage collected heap”?the garbage collected heap is completely allocated and there is no “garbage collected heap” memory available?
Will it throw an exception or allocated somewhere else?
It’s an object allocation like any other. If there’s no memory available and none can be made available through garbage collection, you’ll get an
OutOfMemoryException.You can think of this code:
as being like:
where
Int32_Wrapperwould be the “boxed int” class. (You can’t do this explicitly in C#, but that’s the basic effect.)