Software/C#.Net
Details Regarding Value Types and Reference Types
charom
2011. 5. 15. 08:06
Intriguing Question | Value Type | Reference Type |
Where is this type allocated? | Allocated on the stack | Allocated on the managed heap |
How is a variable represented? | Value type variables are local copies | Reference type variables are pointing to the memory occupied by the allocated instance |
What is the base type? | Must derive from System.ValueType. | Can derive from any other type (except System.Value Type), as long as that type is not “sealed” |
Can this type function as a base to other types? | No. Value types are always sealed and cannot be inherited from. | Yes. If the type is not sealed, it may function as a base to other types |
What is the default parameter passing behavior? | Variables are passed by value (i.e., a copy of the variable is passed into the called function). | For value types, the object is copied-by-value. For reference types, the reference is copied-by-value. |
Can this type override System.Object.Finalize()? | No. Value types are never placed onto the heap and therefore do not need to be finalized. | Yes, indirectly |
Can I define constructors for this type? | Yes, but the default constructor is reserved (i.e.,your custom constructors must all have arguments). | But of course! |