Define: Shallow copy v. Deep copy

  • Thread starter Thread starter Christoph Boget
  • Start date Start date
C

Christoph Boget

I've looked through the online documentation and I
couldn't find a real explanation as to exacly what is
involved with either of the above or exacly what they
mean.
If someone could point me to the proper documentation
that discusses this topic, I'd be ever so appreciative!

thnx,
Christoph
 
From the MS documentation for the Object.MemberwiseClone method:

"A shallow copy creates a new instance of the same type as the original
object, and then copies the non-static fields of the original object. If the
field is a value type, a bit-by-bit copy of the field is performed. If the
field is a reference type, the reference is copied but the referred object
is not; therefore, the reference in the original object and the reference in
the clone point to the same object. In contrast, a deep copy of an object
duplicates everything directly or indirectly referenced by the fields in the
object."

Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
 
As I've heard MemberwiseClone() will be deprecated in .NET Framwork 2.0.

To be replaced with what?

thnx,
Christoph
 
A mistake of me. MemberwiseClone is not really obsolete but it seems that

protected Object MemberwiseClone ()

will be replaced with

protected MarshalByRefObject MemberwiseClone ()

in classes that inherits from MarshalByRefObject, maybe someone else has
more information about it.
 
cody said:
A mistake of me. MemberwiseClone is not really obsolete but it seems that

protected Object MemberwiseClone ()

will be replaced with

protected MarshalByRefObject MemberwiseClone ()

in classes that inherits from MarshalByRefObject, maybe someone else has
more information about it.

That sounds to me like it's just covariant return values. I'll have a
closer look when I get round to installing the Whidbey beta...
 
Haven't heard that, but I do know that ICloneable is widely seen as useless.
 
Haven't heard that, but I do know that ICloneable is widely seen as
useless.

Please elaborate? Why is it widely seen as useless?

thnx,
Christoph
 
Please elaborate? Why is it widely seen as useless?

Because it doesn't specifiy whether or not a deep or shallow copy should be
made. For that reason, if you're handed an ICloneable interface there's no
way to tell what the semantics are. It would be better IMO if there were
specific interfaces for deep and shallow copying.
 
Because it doesn't specifiy whether or not a deep or shallow copy should
be
made. For that reason, if you're handed an ICloneable interface there's no
way to tell what the semantics are. It would be better IMO if there were
specific interfaces for deep and shallow copying.

defining multiple interface would not help either, you never know *how deep*
exactly the object is copied which fields are just bitwise copied and which
are cloned.
therefore is is better not to define any interfaces for that in the
framework at all, this should be done in the specific applications
separately.
 
cody said:
A mistake of me. MemberwiseClone is not really obsolete but it seems that

protected Object MemberwiseClone ()

will be replaced with

protected MarshalByRefObject MemberwiseClone ()

in classes that inherits from MarshalByRefObject, maybe someone else has
more information about it.


This is true for System.__ComObject not for System.Object.

Willy.
 
Back
Top