tags:

views:

53

answers:

4

In VB.NET, if I create a class it is a reference-type. But, if that class it chock full of value type properties, how is this handled? If the class is instantied but never filled, I suspect a pointed for to the heap is allocated. But is more space allocated on the stack for all of it's value type properties?

+1  A: 

No. When you allocate it on the heap, each value type requires space on the heap.

The stack just contains a single reference, no matter how many fields are contained in the class.

Reed Copsey
+2  A: 

When you instantiate a reference type it allocates memory for the object in the heap. At a high level, the amount of memory allocated for the object is the sum of the memory necessary to hold the members.

  • Reference Type Members: Only allocates space to hold the reference, not the object it would point to. This takes up 4 bytes (8 on a 64 bit machines)
  • Value Types: Allocates enough space to hold the entire value type value
JaredPar
+1  A: 

Properties doesn't take up space by themselves. Fields do. It's not required to have a one-to-one correspondence between properties and fields.

Having value type fields in a reference type allocates enough memory for the whole value type whether or not you use it. It's like having all the fields in the value type defined directly in the reference type.

Mehrdad Afshari
A: 

If the class is instantiated but never filled does not matter as the value types cannot have a null value. So they would in fact occupy as much memory even though their values are never actually assigned.

Robban