Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
Deep copy, in C#, refers to a technique by which a copy of an object is created such that it contains copies of both instance members and the objects pointed to by reference members.
Deep copy is intended to copy all the elements of an object, which include directly referenced elements (of value type) and the indirectly referenced elements of a reference type that holds a reference (pointer) to a memory location that contains data rather than containing the data itself. Deep copy is used in scenarios where a new copy (clone) is created without any reference to original data.
Deep copy differs from shallow copy in the manner in which the reference type members of the object are copied. While copying the field members of value type in both cases, a bit-by-bit copy of field is performed. When copying fields of reference type, shallow copy involves copying only the reference, whereas in deep copy, a new copy of the referred object is performed.
Deep copy can be illustrated with an example by considering an Employee object having AddressInfo as a member of reference type along with other members of value type. A deep copy of Employee creates a new object, Employee2, with members of value type equal to Employee but references a new object, AddressInfo2 , which is a copy of AddressInfo.
Deep copy can be implemented using any of the following methods:
To implement deep copy: