Hi Guys
NP59 Changes in Color
I got this program from the following website.
http://www.c-sharpcorner.com/UploadFile/rmcochran/chsarp_memory401152006094206AM/chsarp_memory4.aspx
When Shoe is a struct, program is giving following output:
Bill: Dude!, I have a Blue shoe on my right foot, and a Blue on my left foot.
Ted: Dude!, I have a Red shoe on my right foot, and a Red on my left foot.
When Shoe is a class, program is giving following output:
Bill : Dude!, I have a Red shoe on my right foot, and a Red on my left foot
Ted : Dude!, I have a Red shoe on my right foot, and a Red on my left foot
In above when Shoe is a class, Blue changes to Red this has been explained but I couldn’t understand it. If anyone knows alternative way please explain the reason.
Thank you
Posted Nov 21, 2007, 8:13 AM
Thank you, Alan
AlanPosted Nov 20, 2007, 6:49 PM
Well, there are two Dude objects and hence two similar blocks for Dude in both diagrams.
In the second diagram (no cloning), there are just the two Shoe objects which both Dude objects contain pointers to.
In the third diagram (after cloning), the Shoe objects have been cloned and so there are now four Shoe objects. The first Dude object has pointers to the two original Shoe objects and the other Dude object has pointers to the clones.
Posted Nov 20, 2007, 4:34 PM
Posted Nov 20, 2007, 3:40 PM
Thank you very much for the explanation, Alan
AlanPosted Nov 20, 2007, 1:03 PM
The Dude class contains two fields, LeftShoe and RightShoe, of type Shoe. Shoe itself contains one field, Color, of type string. As Dude is a class, Dude objects are always stored on the heap.
Now when Shoe is a struct (i.e. a value type), a Dude object contains the Shoe objects directly.
However, when Shoe is a class (i.e. a reference type), a Dude object does not contain the Shoe objects directly but a pointer to where they are stored elsewhere on the heap.
When the Dude object named Bill is created, his LeftShoe and RightShoe fields have their Color field set to "Blue".
So, when the CopyDude() method is called to create a new Dude (named Ted) from Bill, to begin with Ted's LeftShoe and RightShoe fields have their Color field set to "Blue" as well.
However, when Ted's LeftShoe and RightShoe fields have their Color field set to "Red" what happens to the Color of Bill's shoes depends on whether Shoe is a struct or a class.
If Shoe is a struct, then there is no effect on Bill's shoes (still "Blue") because the Ted object is storing its Shoes directly. So whatever happens to Ted's Shoes doesn't effect Bill's Shoes because there's no relationship between the two once the copy has been made.
If Shoes is a class then, when the copy is made, the Ted object gets a pointer to where Bill's Shoes objects are stored not a copy of the objects themselves. So, anything that happens to Ted's shoes (changing their Color to "Red") changes Bill's Shoes as well because they both point to the same objects.
The way around this is to 'clone' rather than copy reference type fields so that a completely new object is pointed to, not the original one.