Here is good explanation of what is the difference between both type of copying objects:
http://codeidol.com/csharp/csharpckbk2/Classes-and-Structures/Building-Cloneable-Classes/
Here is simple console app for test:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
-----------------------
private static void Main()
{
A a = new A(1, true, 1m, 5);
A ashallow = a.ShallowCopy();
A adeep = a.DeepClone();
a.ReferenceProperty.DecimalProperty = 2m;
Console.WriteLine(ashallow.ReferenceProperty.DecimalProperty);
//this will print 2 because ashallow have a pointer to referenceproperty of a
Console.WriteLine(adeep.ReferenceProperty.DecimalProperty);
//this will print 1 because adeep have a new instance created with values of referenceproperty of a
Console.ReadLine();
}
//Definition of classes:
[Serializable]
public class A : object
{
private int _IntProperty;
public int IntProperty
{
get { return _IntProperty; }
set { _IntProperty = value; }
}
private bool _BoolProperty;
public bool BoolProperty
{
get { return _BoolProperty; }
set { _BoolProperty = value; }
}
private B _ReferenceProperty;
public B ReferenceProperty
{
get { return _ReferenceProperty; }
set { _ReferenceProperty = value; }
}
public A(int intvalue, bool boolvalue, decimal decimalvalue, short shortvalue)
{
_IntProperty = intvalue;
_BoolProperty = boolvalue;
_ReferenceProperty = new B(decimalvalue, shortvalue);
}
public A DeepClone()
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, this);
m.Position = 0;
return (A)b.Deserialize(m);
}
public A ShallowCopy()
{
return (A)MemberwiseClone();
}
}
[Serializable]
public class B : object
{
private decimal _DecimalProperty;
public decimal DecimalProperty
{
get { return _DecimalProperty; }
set { _DecimalProperty = value; }
}
private short _ShortProperty;
public B(decimal decimalvalue, short shortvalue)
{
_DecimalProperty = decimalvalue;
_ShortProperty = shortvalue;
}
public short ShortProperty
{
get { return _ShortProperty; }
set { _ShortProperty = value; }
}
}