R
raylopez99
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep
cloning versus shallow cloning. The scanned pages of this book are
found here: http://www.sendspace.com/file/mjyocg (Word format, 3 pp)
My question, coming from a C++ background where deep copying is done,
is why in C# you would do either deep or shallow copying as suggested
by O'Reilly (using the "ICloneable" inhereited interface), at least
for the .NET framework.
I thought references made deep or shallow copying unnecessary? After
all, there's no copy constructor in C#.NET--so why do we need deep or
shallow copying, and why, at least for shallow copying, can't one use
the normal constructor instead? (that is: GenericClass <T> object =
new GenericClass <T> ( ); )
RL
File Name: Cloning.doc
Size: 54KB | Description: C# Cookbook 3.26
http://www.sendspace.com/file/mjyocg
Discussion
Cloning is the ability to make an exact copy (a clone) of an instance
of a type. Clon¬ing may take one of two forms: a shallow copy or a
deep copy. Shallow copying is relatively easy. It involves copying the
object that the Clone method was called on.
The reference type fields in the original object are copied over, as
are the value-type fields. This means that if the original object
contains a field of type StreamWriter, for instance, the cloned object
will point to this same instance of the original object's
StreamWriter; a new object is not created.
There is no need to deal with static fields when performing a cloning
operation. There is only one memory location reserved for each static
field per class, per application domain. Besides, the cloned object
will have access to the same static fields as the original.
Support for shallow copying is implemented by the MemberwiseClone
method of the Object class, which serves as the base class for
all .NET classes. So the following code allows a shallow copy to be
created and returned by the Clone method:
public object Clone( ) {
return (this.MemberwiseClone( ))
}
Making a deep copy is the second way of cloning an object. A deep copy
will make a copy of the original object just as the shallow copy does.
However, a deep copy will also make separate copies of each reference
type field in the original object. Therefore, if the original object
contains a StreamWriter type field, the cloned object will also
contain a StreamWriter type field, but the cloned object's
StreamWriter field will point to a new StreamWriter object, not the
original object's StreamWriter object.
Support for deep copying is not automatically provided by the Clone
method or the .NET Framework. Instead, the following code illustrates
an easy way of implementing a deep copy:
BinaryFormatter BF = new BinaryFormatter( )j MemoryStream memStream =
new MemoryStream( )j
BF.Serialize(memStream, this)j memStream.Flush( )j memStream.Position
= OJ
return (BF.Deserialize(memstream»j
Basically, the original object is serialized out to a memory stream
using binary serialization, then it is deserialized into a new object,
which is returned to the caller. Note that it is important to
reposition the memory stream pointer back to the start of the stream
before calling the Deserialize method; otherwise, an exception
indicating that the serialized object contains no data will be thrown.
Performing a deep copy using object serialization allows the
underlying object to be changed without having to modify the code that
performs the deep copy. If you performed the deep copy by hand, you'd
have to make a new instance of all the instance fields of the original
object and copy them over to the cloned object. This is a tedious
chore in and of itself. If a change is made to the fields of the
object being cloned, the deep copy must also change to reflect this
modification. Using serialization, you rely on the serializer to
dynamically find and serialize all fields contained in the object. If
the object is modified, the serializer will still make a deep copy
without any code modifications.
cloning versus shallow cloning. The scanned pages of this book are
found here: http://www.sendspace.com/file/mjyocg (Word format, 3 pp)
My question, coming from a C++ background where deep copying is done,
is why in C# you would do either deep or shallow copying as suggested
by O'Reilly (using the "ICloneable" inhereited interface), at least
for the .NET framework.
I thought references made deep or shallow copying unnecessary? After
all, there's no copy constructor in C#.NET--so why do we need deep or
shallow copying, and why, at least for shallow copying, can't one use
the normal constructor instead? (that is: GenericClass <T> object =
new GenericClass <T> ( ); )
RL
File Name: Cloning.doc
Size: 54KB | Description: C# Cookbook 3.26
http://www.sendspace.com/file/mjyocg
Discussion
Cloning is the ability to make an exact copy (a clone) of an instance
of a type. Clon¬ing may take one of two forms: a shallow copy or a
deep copy. Shallow copying is relatively easy. It involves copying the
object that the Clone method was called on.
The reference type fields in the original object are copied over, as
are the value-type fields. This means that if the original object
contains a field of type StreamWriter, for instance, the cloned object
will point to this same instance of the original object's
StreamWriter; a new object is not created.
There is no need to deal with static fields when performing a cloning
operation. There is only one memory location reserved for each static
field per class, per application domain. Besides, the cloned object
will have access to the same static fields as the original.
Support for shallow copying is implemented by the MemberwiseClone
method of the Object class, which serves as the base class for
all .NET classes. So the following code allows a shallow copy to be
created and returned by the Clone method:
public object Clone( ) {
return (this.MemberwiseClone( ))
}
Making a deep copy is the second way of cloning an object. A deep copy
will make a copy of the original object just as the shallow copy does.
However, a deep copy will also make separate copies of each reference
type field in the original object. Therefore, if the original object
contains a StreamWriter type field, the cloned object will also
contain a StreamWriter type field, but the cloned object's
StreamWriter field will point to a new StreamWriter object, not the
original object's StreamWriter object.
Support for deep copying is not automatically provided by the Clone
method or the .NET Framework. Instead, the following code illustrates
an easy way of implementing a deep copy:
BinaryFormatter BF = new BinaryFormatter( )j MemoryStream memStream =
new MemoryStream( )j
BF.Serialize(memStream, this)j memStream.Flush( )j memStream.Position
= OJ
return (BF.Deserialize(memstream»j
Basically, the original object is serialized out to a memory stream
using binary serialization, then it is deserialized into a new object,
which is returned to the caller. Note that it is important to
reposition the memory stream pointer back to the start of the stream
before calling the Deserialize method; otherwise, an exception
indicating that the serialized object contains no data will be thrown.
Performing a deep copy using object serialization allows the
underlying object to be changed without having to modify the code that
performs the deep copy. If you performed the deep copy by hand, you'd
have to make a new instance of all the instance fields of the original
object and copy them over to the cloned object. This is a tedious
chore in and of itself. If a change is made to the fields of the
object being cloned, the deep copy must also change to reflect this
modification. Using serialization, you rely on the serializer to
dynamically find and serialize all fields contained in the object. If
the object is modified, the serializer will still make a deep copy
without any code modifications.