Click here to Skip to main content
15,513,489 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
As I have been diving deep into the topic of boxing it got me rethinking on how reference type variables work, specifically in terms of how the value that is referenced by them is stored in the heap. This is a silly question but any clarification is appreciated.

public class Program
    public static void Main(string[] args)
        CustomClassType inst1 = new CustomClassType();

public class CustomClassType

Thus, concerning my above example: I am aware of what goes on on the left side of the assignment operator. However, it's what is on the right side that I am a bit confused on. Thus, my question is, when an instance of user-defined class type is CustomClassType is created, is the instance stored directly in the heap as is? Or is the instance stored in a box (per say, as with boxing a value type) and then stored in the Heap?

I am aware that a referece to something is assigned to variable inst1. However, it's about me trying to understand what that something is. In terms of if that something is an instance (stored in the heap) or if that something is a box (as with in boxing a value) storing an instnance.

And please include if the same applies to literal values of reference type (e.g. "Bill").

What I have tried:

Any kind of useful clarification is appreciate thanks.
Updated 24-May-21 3:22am
Gerry Schmitz 23-May-21 15:45pm    
"Boxing" applies to value types; creating an object (reference type) to store on the heap. There is no (further) "boxing" of reference types; it would be pointless.

(The "string literal" seems to have an extra dimension ... the "string" table / pool).
[no name] 23-May-21 17:29pm    
In that case are you implying that a value of Reference type is stored directly in the Heap (not stored in some box)? Thus, nothing like how a value that is boxed is stored in the Heap?
Gerry Schmitz 24-May-21 0:32am    
The reference type "is" the "box".

I think you are over-thinking how Reference Types are stored. They go on the heap, and what you "get back" when you instantiate a Reference Type is a pointer to the in-memory location of the instance.

With Types String, Struct, and Enum, things get more complex in terms of internal storage.

See if you find this helpful: [^].

If you want a deeper dive into Type handling, see Eric Lippert's articles: [^], [^], [^] ,[^], [^]
Share this answer
[no name] 23-May-21 3:09am    
In that case the value referenced by a Reference type variable is nothing like a value that is boxed?

In other words, are you saying that a value (whether it be a literal value or an instance) of reference type is stored directly in the Heap, in NO box (per say)? Thus, nothing like a value that is boxed, which is stored in a box (per say) then stored on heap?
BillWoodruff 23-May-21 3:59am    
You have a choice to make: spend time studying the links I sent you and gain a basic knowledge of how Types are allocated, stored ... or, persist in lurching to and 'fro on the deck of a ship in a storm :) Boxing, and unboxing, are simple:
BillWoodruff 24-May-21 1:18am    
Very entertaining :)
enhzflep 24-May-21 21:14pm    
Indeed. Thanks for signposting the route to this fun little cul-de-sac.
only value types are ever boxed. boxing is basically a thunk to allow a value type to be referred to with a double indirected GC pointer and thus tracked by the GC.

value types typically live on the stack so they are not tracked by the GC.

Boxing basically copies a value type from the stack onto the heap in a "box" which is "kind of" a wrapper for a value type (not really an actual wrapper, just a thunk with some bookkeeping info in it)

A class already lives on the heap. Therefore, a class never needs to be boxed. It already has the bookkeeping data in the 80 or so bytes of overhead that a class takes (if i remember correctly, but my memory is iffy on specifics)

And in terms of a reference to a boxed type it's double indirected internally just like a class pointer is (because it's managed by the GC), whereas again, a pointer to a value type on the stack doesn't have to be.

What I am telling you is all implementation details, and may change, or have been changed since I last looked.
Share this answer
Richard Deeming 24-May-21 8:54am    
Be warned, the OP has been kicked off for being a nasty little troll, and has re-spawned at least once so far to hurl abuse at the people who try to help him.
honey the codewitch 24-May-21 8:59am    
If he does, he'll regret it. I didn't post the answer for him. I posted it because it was a good question. I half assumed he'd be kicked for the autism remark.
Dave Kreskowiak 24-May-21 9:32am    
No, he won't. His kind never regrets the attempts at insults they throw.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900