What is the difference between shallow copy and deep copy?
What is the difference between shallow copy and deep copy?
282
19-Jun-2025
Ponu Maurya
23-Jun-2025Definitions
Example
Shallow Copy:
p2.Nameis a new string (value type in effect).p2.Addressis shared withp1— changingp2.Address.Citywill also affectp1.Deep Copy:
p2is fully independent ofp1.p2.Address.Citywill not affectp1.Shallow Copy in .NET:
You can perform a shallow copy using:
This is protected and typically used within a class implementing
ICloneable.Deep Copy Techniques:
Summary Table
Also Read