As most of you know a reference type can locate in managed heap and a value type can locate in stack. But many people want real examples of what make them differentiate in a programmer's world. I tried to find out some tips about this considering four cases. Pure Value Type - which contains Value Type members, Pure Reference Type - which contains only Reference Type members, Value Type With Reference Type - A Value Type which contains both Value Types and Reference Types, Reference Type With Value Type - A Reference Type Which contains both Value Type and Reference Type. You can see the differences when you assign one instance of a particular type to another instance of the same type. The core thing is that during an assignment whether it assigns only a value or it assigns both value and reference (memory location). Check the cases I mentioned below. Please download the source code along with this to find more your self [I used VS2005 WebProject Template]. Let consider each case briefly. Pure Value Type - Here I used a structure as a value type. It has a an integer member. I created two instances of this structure. After wards I assigned second instance to the first one. Then I changed the state of second instance, but it hasn't effect the first one, as whole items are value type and assignments on those types will copy only values not references i.e. in a Value Type assignment, all instances have it's own local copy of members. Pure Reference Type - I created a class and added a "DataTable" as a Reference Type member for this class. Then I performed the assignments just like below. But the difference is that on changing the state of second instance, the state of first instance will automatically alter. So in a Reference Type assignment both Value and Reference will be assigned i.e. all instances will point to the single object. Value Type With Reference Type - This case and the last case to come are more interesting. I used a structure in this particular scenario also. But this time it includes a Reference Type(A Custom Class Object) Member besides a Value Type (An Integer) Member. When you performing the assignments, it seems like a swallow copy, as Value Type member of first instance won't effected, but the Reference Type member will alter according to the second instance. So in this particular scenario, assignment of Reference Type member produced a reference to a single object and assignment of Value Type member produced a local copy of that member. Reference Type With Value Type - Contrary to the above case, in this scenario, both Reference & Value Types will be effected. i.e. a Value Type member in a Reference Type will be shared among it's instances. Happy coding.
As most of you know a reference type can locate in managed heap and a value type can locate in stack. But many people want real examples of what make them differentiate in a programmer's world.
I tried to find out some tips about this considering four cases.
You can see the differences when you assign one instance of a particular type to another instance of the same type. The core thing is that during an assignment whether it assigns only a value or it assigns both value and reference (memory location). Check the cases I mentioned below. Please download the source code along with this to find more your self [I used VS2005 WebProject Template].
Let consider each case briefly.
Happy coding.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: