Monday, 25 June 2012

VALUE TYPE & REFERENCE TYPE

Q: Difference between Value Type & Reference Type:


Ans: Value type  stores the datawhile a reference type stores a reference to the data. 


The value of value types are stored on the managed stackand can be used directly.
This means that the value is stored, and not a reference to the value.


This also means that each value type has its own copy of the data.




(1) An instance of value type, called a value
(2) An instance of the reference type, called an object, is allocated and managed on the Garbage Collection heap


--------------------------------------


Reference types store the address of their data on the stackThe actual data that the address refers to is stored in an area of memory called the heap


****************************************************************
NOTES:


(1) All value types are derived from System.ValueType.
(2) You cannot derive new type from value type.
(3) Struct can implement Interface.






Why String is a reference type while Interger is a Value type


A string does not have a pre-defined memory size, so it requires dynamic memory.  When a string object is created, the actual value is stored within dynamic memory, or on the heapTo access it, a reference to this memory space is stored in the stack, thus the name "reference type".







No comments:

Post a Comment