How to implement cloning in .NET ? What is shallow copy and deep copy

Q

How do you implement prototype pattern in .NET?

✍: Guest

A

Cloning is achieved by using ICloneable of the System namespace. It has a “Clone” method which actually returns the reference of the same copy. Clone method allows a Shallow copy and not a deep copy. In Shallow copy if you make changes to the cloned object it actually changes on the main object itself. So how is deep copy achieved, by using “ISerializable” interface? So what you do is first serialize the object then deserialize back to a complete new copy. Now any changes to this new copy do not reflect on the original copy of the object, this is called as Deep copy.

2007-10-24, 5265👍, 0💬