0

Possible Duplicate:
What is the difference between a deep copy and a shallow copy?

I saw this today here: http://msdn.microsoft.com/en-us/library/system.web.routing.route.aspx and one of the member functions was:
"MemberwiseClone - Creates a shallow copy of the current Object. (Inherited from Object.)"
So whats a "shallow copy" verse a ... "Deep copy"?

Community
  • 1
  • 1
Letseatlunch
  • 2,432
  • 7
  • 28
  • 33

1 Answers1

0

Shallow copy only replaces the properties on the current level of the object, that means if you have an object as a property it will have the same reference as your original. This is not a problem if your properties are value types or primitives of course.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • How did this answer get through o.O – H.B. Feb 13 '11 at 17:39
  • I barely understood what that means? does it mean shallow copy doesn't actually copy anything? just passes references? In a Shallow copy both objects will act as the same object you mean? – SSpoke Jun 26 '11 at 01:25
  • @SSpoke: Well, this can be illustrated with collections, if you have a shallow copy of a collection you have two collection where both point towards the same elements, if it's a deep copy all the elements in the copied collection are also copies. A shallow copy does copy something: The references. Also see [the duplicate question](http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy) and [Wikipedia](http://en.wikipedia.org/wiki/Shallow_copy#Shallow_copy). – H.B. Jun 26 '11 at 01:32
  • Well I ended up using MemberwiseClone() I'm guessing thats what I need, I'm trying to fix a bug here where two objects I both use assignment operator `=` on them.. and they both start modifying each other that makes me pull my hair out because I want them to just hold coordinates.. people tell me use struct, instead of class I cannot use struct as I use the class for XML de-serialization so I hope this shallow copy is what i need! so far i think it's working fine i have to run some tests later. – SSpoke Jun 26 '11 at 05:29
  • @SSpoke: This is one of the basic concepts you have to understand when programming, that using an assignment on reference types does not copy the object but that both references will point to the same instance of an object. – H.B. Jun 26 '11 at 08:59
  • Yes i understand that but after reading wikipedia about shallow copy vs deep copy seems shallow copy does same thing as assignment where both pointers get copied into one and sharing happens.. pretty confusing.. (since i am not having the assignment `=` bug), I know deep copy would copy all inner-classes that are shared in the object you are trying to clone i don't know how and really I have no use for that anyways. Any who seems wikipedia is wrong or i misinterpreted wrong. But it seems to be working fine now.. I hope it's not just getting lucky, then again a clones job is to clone! thanks :) – SSpoke Jun 26 '11 at 11:32