Object.MemberwiseClone Method Creates a shallow copy of the current Object.
using System;
publicclass IdInfo
{
publicint IdNumber;
public IdInfo(int IdNumber)
{
this.IdNumber = IdNumber;
}
}
publicclass Person
{
publicint Age;
public string Name;
public IdInfo IdInfo;
public Person ShallowCopy()
{
return (Person)this.MemberwiseClone();
}
public Person DeepCopy()
{
Person other = (Person) this.MemberwiseClone();
other.IdInfo = new IdInfo(this.IdInfo.IdNumber);
return other;
}
}
publicclass Example
{
publicstaticvoid Main()
{
Person p1 = new Person();
p1.Age = 42;
p1.Name = "Sam";
p1.IdInfo = new IdInfo(6565);
Person p2 = (Person) p1.ShallowCopy();
}
}