Shallow and Deep Copy

What is shallow and deep copy in C#? How to perform the same in C#?

Questions by a_sahayam_811572   answers by a_sahayam_811572

Showing Answers 1 - 6 of 6 Answers

gopi2008

  • Apr 20th, 2008
 

shallow copy copies the structure of the object, deep copy copies structure as well as data.

For example, consider an object called X that references objects A and B. Object B, in turn, references object C. A shallow copy of X creates new object X2 that also references objects A and B. In contrast, a deep copy of X creates a new object X2 that references the new objects A2 and B2, which are copies of A and B. B2, in turn, references the new object C2, which is a copy C. Use a class that implements the ICloneable interface to perform a deep or shallow copy of an object.

  Was this answer useful?  Yes

Shallow copy make a copy of data along with it's reference, for example object A has property name, by making a shallow copy to object B, and changes the name property value that will reflect in object A.

Deep copy is one which copies only the data and not it reference. there will not be any impact on name property of object A when the object B's chnaged.

To achive deep copy IClonable.Clone() method needs to implmented. which shloud have the implementation of this.memberwiseClone().

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions