Home   Cover Cover Cover Cover
 

Value types and reference types

Question: What is the fundamental difference between a value type and a reference type? What effect does this difference have at run time?

Answer: see 3.2.2 Value Types

The data of value types is stored directly on the stack, while reference type variable only hold a pointer to the data on the heap.

Removing the value type variable from the stack frees the storage space its data occupied. In the case of reference variable only the storage space for the object pointer will be freed. The memory occupied by the data will remain on the heap until the garbage collector runs.

Therefore and because each access to the data of a reference type involves an addition redirection to the heap, reference types cost more runtime. This is why value types are especially suited for short-lived intermediate results.