static void DeepCopyClass() { WriteLine("\nDeepCopyClass()"); WriteLine("Shallow Copy"); { MyClassA source = new MyClassA(); source.MyField1 = 10; source.MyField2 = 20; MyClassA target = source; target.MyField2 = 30; WriteLine($"{source.MyField1} {source.MyField2}"); WriteLine($"{target.MyField1} {target.MyField2}"); } WriteLine("Deep Copy"); { MyClassA source = new MyClassA(); source.MyField1 = 10; source.MyField2 = 20; MyClassA target = source.DeepCopy(); target.MyField2 = 30; WriteLine($"{source.MyField1} {source.MyField2}"); WriteLine($"{target.MyField1} {target.MyField2}"); } }
public MyClassA DeepCopy() { MyClassA newCopy = new MyClassA(); newCopy.MyField1 = this.MyField1; newCopy.MyField2 = this.MyField2; return(newCopy); }