Varad, The .NET Guy!

Exploring the excitement of Microsoft .NET and much more..

C#: Shallow Copy

In C#, the shallow copy is also referred as memberwise copy.
 
A shallow copy creates a new instance of the same type as the original object, and then copies the nonstatic 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. In contrast, a deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object.

For example, if X is an Object with references to the objects A and B, and the object A also has a reference to an object M, a shallow copy of X is an object Y, which also has references to objects A and B. In contrast, a deep copy of X is an object Y with direct references to objects C and D, and an indirect reference to object N, where C is a copy of A, D is a copy of B, and N is a copy of M.

The Type of the clone is the same as the type of the original Object.

Out of Box, there are 3 types of Copy Constructor in C# 1) Reference Copy 2) Memberwise copy or Shallow copy and 3) Deep copy.

To learn more about C# Copy Constructor check out this nice article

http://www.phptr.com/articles/printerfriendly.asp?p=25352

 

Posted: Sep 13 2004, 11:03 PM by Varad | with no comments
Filed under:

Comments

No Comments