Blog Home  Home Feed your aggregator (RSS 2.0)  
Software Code Help - what is deep copy and shallow copy in c#
Blog
 
# Wednesday, October 07, 2009

A shallow copy creates a new instance of the same type as the original object, and then copies the non-static fields of the original object. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not; therefore, the reference in the original object and the reference in the clone point to the same object.

A deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object

Wednesday, October 07, 2009 7:19:22 AM (GMT Daylight Time, UTC+01:00)  #    Comments [1]   C# | Interview Question .Net  | 
Wednesday, October 07, 2009 7:47:52 AM (GMT Daylight Time, UTC+01:00)
A shallow copy of an object copies all of the member field values. The pointer will be copied. but the memory it points to will not be copied,the field in both the original object and the new copy object will then point to the same allocated memory. You can use this using MemberwiseClone() of C#.

If you changed in the new object then its changed value is also reflected into the old one.

A deep copy copies all fields, and makes copies of allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor. like new Object(Value)

If you changed in new object then it will not reflected into the old object
Comments are closed.
Copyright © 2010 SoftwareCodeHelp. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: